Learning how to use the command line on a Linux machine is pretty critical for almost every developer.
The first commands you might use are outlined below.
These are all ways of representing a directory that you can pass to the program you are using. For example -
cd followed by all of the above would be valid.
..is the parent directory.
./is the current directory.
/is the root directory.
~/is your home directory.
You can use absolute or relative paths for any argument to a program, for example
~/Documents would be the same as relative path
./Documents (if you are in the home directory), and the same as absolute path
| character is called a pipe. You can 'pipe' the output of one program into another. For example, you may wish to view the contents of a file, sorted:
cat filename | sort
You can do this multiple times, so if you wish to view unique lines:
cat filename | sort | uniq
< are used to redirect output or input.
< are used for overwriting.
<< are used for appending.
For example, if we wish to write the output of our above set of chained commands:
cat filename | sort | uniq > output_file.txt
Similarly, we may wish to append to a file:
cat filename | sort | uniq >> output_file.txt
The input redirection can be used as such (example importing a mysql database):
mysql -u root -p dbname < db.sql
Arguments are used generally to modify the behaviour of a command. For example, adding the argument
-v usually means verbose - eg print more info.
So you could copy a file as such:
cp filename /to/this/place/here/
But you may wish to add additional arguments, for example to copy recursively and be more verbose:
cp -rv folder_name /to/this/place/here
In a Linux program, the argument array is the list of arguments passed to a program. This list can usually be as long as you like.
This is very useful, for example - you may wish to edit all files with a certain string in them.
$(command goes here) will make the output of
command goes here an argument / arguments to a second program.
An example is to open all files containing a string with vim, or another text editor:
vim $(grep -rl "search_string" ./)
cd [directory] command will change your current working directory.
For example, if you open up a terminal you will by default be in
To change the working directory to
/home/user/Documents, you could use:
Now, to go up to a parent directory you would use
... So to go from
/home, you would use:
The following would also achieve the same task.
When you are navigating using
cd, you probably want to know where you are. Running
pwd will print your working directory.
Using the above
pwd > /home/user cd Documents pwd > /home/user/Documents cd ../../ pwd > /home
ls will list all the files in the current directory.
It's fairly straight forward. Often you might use the
-lah flags to show more information such as filesize, date last modified, permissions and user / group.
mv command will move a file to another directory.
Use is at such:
mv filename /home/my_awesome_directory/
The above will move filename from the current working directory to
You can also use absolute filepaths, as such:
mv /tmp/my_awesome_file /home/my_awesome_directory
cp command will copy files from one location to another.
To copy a directory and its contents you must use the recursive flag
-v to be more verbose.
cp -rv /my/foldername /to/this/location
Want to know more about a command? Type
man commandname to read the user manual. For example,
To make a directory, you can use
Remember, you can always use relative and absolute file paths. The following:
cd /tmp mkdir dir_name
Would be the same as
To remove a file, just run
To remove a directory and its contents, use
rm -rf directory_name. Be careful with this.
Use this to remove an empty directory - for example
To create a file, just use
This is useful when you've got a huge amount of garbage on your screen and want the cursor to go back to the top. Just
clear, no other arguments necessary.
Generally speaking there are a couple of
chmod commands you will use regularly. I'm not going to go into great detail as you can read the manual if you want more info (
man chmod) - but:
chmod 755 filename: change filename to permissions
chmod -R xxx: recursively update files to permissions
xxx- where xxx is 755, 400, etc.
To change the owner and group run
chown owner:group filename.
You can combine this with the recursive flag to update all files under a directory:
chown -R owner:group folder_name