Linux Basics

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.


What is .., ./, /, ~/?

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 /home/username/Documents.

What is piping?

The | 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

What is redirection?

The characters > and < are used to redirect output or input.

> and < are used for overwriting.

>> and << 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

What are arguments?

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.

How can I use the output of one program as an argument to another?

This is very useful, for example - you may wish to edit all files with a certain string in them.

Using $(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" ./)

Change Directory (cd)

The cd [directory] command will change your current working directory.

For example, if you open up a terminal you will by default be in /home/user.

To change the working directory to /home/user/Documents, you could use:

cd ./Documents

Now, to go up to a parent directory you would use ... So to go from /home/user/Documents to /home, you would use:

cd ../../

The following would also achieve the same task.

cd /home

Print Working Directory (pwd)

When you are navigating using cd, you probably want to know where you are. Running pwd will print your working directory.

Using the above cd example:

> /home/user
cd Documents
> /home/user/Documents
cd ../../
> /home

List Files (ls)

Running ls will list all the files in the current directory.

It's fairly straight forward. Often you might use the -l or -lh or -lah flags to show more information such as filesize, date last modified, permissions and user / group.

Move Files (mv)

The 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 /home/my_awesome_directory/

You can also use absolute filepaths, as such:

mv /tmp/my_awesome_file /home/my_awesome_directory

Copy Files (cp)

The cp command will copy files from one location to another.

To copy a directory and its contents you must use the recursive flag -r. Use -v to be more verbose.

For example:

cp -rv /my/foldername /to/this/location

Read the Manual (man)

Want to know more about a command? Type man commandname to read the user manual. For example, man ssh.

Make a Directory (mkdir)

To make a directory, you can use mkdir directory_name

Remember, you can always use relative and absolute file paths. The following:

cd /tmp 
mkdir dir_name

Would be the same as

mkdir /tmp/dir_name

Remove a File (rm)

To remove a file, just run rm filename.

To remove a directory and its contents, use -rf flags: rm -rf directory_name. Be careful with this.

Remove a Directory (rmdir)

Use this to remove an empty directory - for example rmdir directory_name.

Make an Empty File (touch)

To create a file, just use touch empty_file_name_here.

Clear the Terminal Screen (clear)

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.

Change Permissions (chmod)

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:

  1. chmod 755 filename: change filename to permissions 755
  2. chmod -R xxx: recursively update files to permissions xxx - where xxx is 755, 400, etc.

Changing Owner (chown)

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

Checkout our Web Scraping Service