Linux Command Line – a Primer

Linux Command LineFor any technician coming from the Windows world, where almost anything can be achieved by moving, clicking, and dragging icons, nodes, or windows, the Linux command line can seem a little daunting.

This primer should help break down some barriers.

You’ve just managed to log in to a Linux Shell…

…so the first thing to keep in mind that you are in your “home” directory.

If you have logged in as any user EXCEPT the “root” user, your home directory is “/home/<username>” (eg. “/home/kevin”).

If you have logged in as the “root” user, your home directory is “/root”.

Change the Working Directory

You can use the “cd” command to navigate the directory structure. The “cd” command has two main forms.

cd <target>

You can issue the “cd” comment specifying the target directory you wish to navigate to. You can specify an absolute target, or a relative target.

Absolute Targets

You can specify the target folder by entering the complete directory path. So:

cd /var/mail

…will change your current working directory to “/var/mail”.

Relative Targets

You can specify the target folder by entering the path relative to the current directory path. To do this, keep in mind that the special shortcut denoted by “..” actually translates to “the parent of the current directory”.

So, if your current directory is “/home/kevin”, and you wish to navigate to “/home/joseph”, you can use the “relative” target method:

cd ../joseph

cd – without arguments

If you issue the “cd” command with no additional arguments, you will be returned to your home directory.

About the Home Directory

You should also keep in mind that the shortcut character “~” means “the current user’s home folder”.

So if you issue the command “cd ~/documents”, this would be the equivalent of “cd /home/kevin/documents” (for the user “kevin”) or “cd /root/documents” (for the user “root”).

Which Directory am I in?

Simply issue the “pwd” (print working directory) to get this information:

kevin@debian:~$ pwd
/home/kevin
kevin@debian:~$ ls
notes2.txt notes3.txt other
kevin@debian:~$ cd other
kevin@debian:~/other$ pwd
/home/kevin/other
kevin@debian:~/other$

In most Linux environments, however, the command line prompt already shows the current working directory – and this can indeed be seen in the above example (the “~” in the prompt denotes the user’s home directory)

Listing Directory Contents

You can use the “ls” command to list the contents of the current working directory. The simplest form is issue the command without any arguments. This can yield output such as the following:

kevin@debian:~$ ls
notes1.txt notes2.txt other
kevin@debian:~$

The shell will typically indicate a directory using coloured text, but the directory listing is quite terse, and not very informative. You can however invoke the “long listing format” using the “-l” option:

kevin@debian:~$ ls -l
total 12
-rw-r–r– 1 kevin kevin 35 Dec 31 08:55 notes1.txt
-rw-r–r– 1 kevin kevin 35 Dec 31 08:56 notes2.txt
drwxr-xr-x 2 kevin kevin 4096 Dec 31 08:56 other
kevin@debian:~$

This format shows us a number of columns, containing:

  • filetype and permission flags
  • link count
  • the username that owns the file/directory
  • the groupname that owns the file/directory
  • the file size
  • last modified date
  • file/directory name

Hidden Items

To show hidden items in a directory listing, you can issue the “ls” command with the “-a” option. So, following our current example, we can also combine the “-l” option to obtain the following:

kevin@debian:~$ ls -al
total 36
drwxr-xr-x 3 kevin kevin 4096 Dec 31 08:57 .
drwxr-xr-x 3 root root 4096 Dec 31 08:37 ..
-rw——- 1 kevin kevin 217 Dec 31 09:50 .bash_history
-rw-r–r– 1 kevin kevin 220 Dec 26 17:14 .bash_logout
-rw-r–r– 1 kevin kevin 3515 Dec 26 17:14 .bashrc
-rw-r–r– 1 kevin kevin 675 Dec 26 17:14 .profile
-rw-r–r– 1 kevin kevin 35 Dec 31 08:55 notes1.txt
-rw-r–r– 1 kevin kevin 35 Dec 31 08:56 notes2.txt
drwxr-xr-x 2 kevin kevin 4096 Dec 31 08:56 other
kevin@debian:~$

It should become obvious from the output listed above that, in the Linux world, hidden files start with a period “.” character.

Copying Files

The “cp” command has a simple enough format to copy files:

cp <target> <destination>

So the example command “cp notes1.txt notes3.txt” would make a copy of a file named “notes1.txt” inside the current directory to a new file “notes3.txt”, also in the current directory.
The destination does not necessarily need to be a file name, but can also be a folder name. Therefore, the example command “cp notes1.txt other” would make a copy of a file named “notes1.txt” inside the current directory into a subdirectory named “other”. Note that if the “other” folder does not exist, the result would be the creation of a copy of the “notes1.txt” file with the new name “other”!

You should also be aware that some Linux distributions will overwrite the destination if it exists without prompt or warning. This document was written with Debian 8 (Jessie) as a target, and indeed it will overwrite files without warning.

Moving Files Around

Moving of files and folders is accomplished with the “mv” command:

mv <target> <destination>

The command “mv notes1.txt other” would move the file “notes1.txt” out of the current directory into the subdirectory called “other”. You should keep in mind that, in this example, if the destination directory “other” does not exist, you will achieve the effect of renaming the file from “notes1.txt” to “other”. In fact, the “mv” command is the tool used to rename files and folders.

Another useful thing to remember is that you can specify a directory name as a target. This means that you can move a directory and all its contents with a single command; for example, “mv /usr/local/myfolder /home/kevin” would move the “/usr/local/myfolder” directory out of “/usr/local” and relocate it to become “/home/kevin/myfolder”.

Create an Empty Directory

This is simple enough:

mkdir <target>

Removing Files and Directories

Removing files and directories should be done with care, since there is no normal way to retrieve the lost data. The format of the command is:

rm <target>

…where the target can be a file or a directory.

To remove a file, you would use something like the below:

rm notes1.txt

or

rm /home/kevin/other/notes3.txt

To remove an empty directory, you would again specify the directory to remove:

rm /home/kevin/other

…but if there is anything inside that directory the command will be rejected. If you need to remove a directory AND its contents, then you would need to specify the “-r” option (meaning recursive):

rm -r /home/kevin/other

Use with care!

Editing Text Files

The “nano” command is arguably today’s most popular (user-friendly-ish) text editor used at the Linux command line. The format:

nano <target-file>

…will open up the file for editing – but do keep in mind that if the file you open is NOT a plain-text file, you may well damage the file when you save it.

Incidentally, you would use “CTRL+O” to save the file, and “CTRL+X” to save and exit.

Do keep in mind that if the specified target does not exist, nano will create a new empty file for you to start typing into.

 

Discuss this article with our community:
0 Comments