Using the Linux command line

The Linux command line (or shell) is a powerful tool that can be used to automate jobs or complete jobs more quickly than using a GUI (graphical user interface). It is also used to control computers which do not have a monitory, like a web server.

To use it open the terminal application on your Linux installation from your applications menu.

Simple usage

This section introduces some common commands used. These commands should work in both the “zsh” and “bash” shells.

Directories

When the terminal is opened it will set to work at your home folder/directory. Which is normally the path “/home/[username]/”. The directory you are working is called your working directory, and any commands entered that depend on what directory you are in use that directory. How to change to another directory is covered later:

[username]@[computer-name] ~ $

Typing ‘ls’ and then pressing the [ENTER] key shows all the files (that includes directories as a directory is a kind of file) in your home directory (probably different files to the below). The symbol ‘~’ represents your home directory.

~ $ ls
Desktop Documents Downloads Pictures Videos

When a command has been typed it is necessary to press [ENTER] to run it.
If you mistype or request a command to do something it cannot do, like delete /sbin/init, it will fail with an error message. When there are no problems a command will often print nothing and quietly complete its task.

Most commands can take arguments, which are text that the software run by the command interprets to change its default behaviour. The arguments are typed just after the command.

The ‘ls’ command can take the name of a directory as an argument, the files in the named directory will listed instead of the working directory. If an argument has spaces, enclose it in "", or it won’t work:

~ $ ls
Desktop Documents Downloads Pictures Videos
~ $ ls Documents
Additional_files letter.odt presentation.odp tax_returns.ods timetable.odt

The above shows it for the fictional ‘Documents’ directory which is found in the home directory (‘~’). Using ‘~’ is the same as typing the full location of your home directory.

Directories and files can be referenced relative to the current working directory, CWD, (currently ~), and the folder will be found in the CWD. An absolute path (which starts with ‘/’) is unaffected by the CWD. It specifies the files exact location. The Documents directory absolute path would be “/home/[username]/Documents”, and using relative directories. Examples:

~ $ ls /home/[username]/Documents
Additional_files letter.odt notes.txt presentation.odp tax_returns.ods timetable.odt
~ $ ls ./ # "ls ~" also list the files in your home directory
~ $ ls Documents/../ #Lists one directory above Documents, this dir.
Desktop Documents Downloads Pictures Videos

Effectively the command prompt adds “/home/[username]” before the location for you. In paths ‘.’ refers to the last mentioned directory in the path and ‘..’ refers to the directory that contains the directory last mentioned. If you haven’t stated a directory, it means the directory containing the CWD

Documents has a subdirectory called “Additional_files” and its contents can be listed:

~ $ ls Documents/Additional_files
draw.odg old_tax_returns.ods

Working from one directory to do work in another is tedious, as a lot of typing is needed to access files in another directory, especially if that is where all the work is being done. Your CWD can be changed using the ‘cd’ command, which takes the directory to change the CWD to as its argument:

~ $ cd Documents # Change Directory to Documents
~/Documents $ ls # Shows files in CWD
Additional_files letter.odt notes.txt presentation.odp tax_returns.ods timetable.odt
~/Documents $ ls Additional_files
draw.odg old_tax_returns.ods
~/Documents $ cd ~ #Change quickly to home directory
~ $

Any text after a “#” is ignored by the shell and known as a comment.

A new directory can be created using ‘mkdir’, with its argument being the directory to create.

Viewing files

You can now navigate directories. Usually you also want to view the content of files in the directories. This is done by using a command that opens the program that can view them. For an .odt files for example you can use loffice to open LibreOffice (if it’s installed):

~/Documents $ loffice timetable.odt # Opens timetable.odt in LibreOffice

To view a plain text file (see plain text editor) the ‘less’ command is used. When you use it you can move up and down the file by using the arrows keys and to exit the viewer press ‘q’.

In Documents the only plain text file is called “notes.txt”. Try passing the name of a plain text file (when in the correct working directory) as an argument to less to see its contents. Some other such files have .cpp, .py, .html, and .conf as the file name ending.

Editing plain text files

Another command nano is used. This is an editor for plain text files that exists in the command line. It takes the name of the file to edit or create as its argument. To create or edit a file called “test.txt” in the Documents folder, for example, use:

~ $ nano Documents/text.txt

Other ways to do the same thing, but using different ways to name the file are:

~ $ cd Documents~/Documents $ nano text.txt

~ $ nano /home/[username]/Documents/test.txt

Anything you type on the keyboard will be added to the file. The arrow keys move the cursor (blinking block) that marks where you’re editing the file. To exit the nano editor use Ctrl+X, enter ‘y’ then [ENTER] to save the file or ‘n’ to discard the changes. You can save anytime using Ctrl+O then [ENTER].

The new file can be then viewed using less mentioned earlier:

~ $ less Documents/text.txt

To create an empty file use the ‘touch’ command, it does nothing if the file exists:

~/Documents $ less text.txt
~/Documents $ touch test
~/Documents $ touch text.txt one.txt # Can be given multiple files to create
~/Documents $ less text.txt
~/Documents $ less test
~/Documents $ cat one.txt
~/Documents $ ls
Additional_files letter.odt notes.txt one.txt presentation.odp tax_returns.ods test text.txt timetable.odt

Deleting files and directories

To delete a file use the ‘rm’ command with the filename as an argument. Be careful as this can’t be easily undone.

Deleting directories is different, to delete one every file inside it must also be deleted. Add the argument -r tells it to recursively delete the directory (deleting each file inside it). This is an option, any options for a command are preceded by ‘-‘:

~ $ rm Documents/notes.txt
~ $ rm -r Downloads

Another option for rm is ‘i’, this makes rm ask you before deleting a file. Multiple options can be after the same ‘-‘ or as separate arguments:

~ $ rm -ri Downloads
[Asks permission to delete each file in Downloads]
~ $ rm -r -i Downloads
[Asks permission to delete each file in Downloads]

An empty directory can be deleted using the ‘rmdir’ command instead.

Moving and renaming

To do this use the ‘mv’ command. It takes the file/directory to move/rename as its first argument and the file/directory’s new place/name as its second argument:

~ $ mv Downloads My_Downloads # Rename/move a directory
~ $ mv Documents/notes.txt Documents/notes1.txt # Renames notes.txt
~ $ mv Documents/notes.txt notes1.txt # Moves notes.txt into the home directory
~ $ cd Documents
~/Documents $ mv notes.txt notes1.txt # Renames notes.txt

Copying

This is done with the ‘cp’ command, like ‘mv’ it takes two file names as arguments. The first argument is the original file and the new filename as the second argument. Unlike ‘mv’, but like ‘rm’ it must be told to copy all the files in a directory when copying one (using the r option). It also has the i option so you are asked before the command does anything.

~ $ cp Documents/notes.txt notes_here.txt # Copy notes.txt to ~
~ $ cp -r Pictures Pictures_copy # Copy all the pictures stored
~ $ cp -ri Pictures Pictures_copy # Copy all the pictures stored, ask for confirmation for each
~ $ cp -i Documents/one.txt ./one.txt # Asks for confirmation to copy one.txt

Getting help

The ‘man’ command provides documentation on almost any command. The command to get help on is the argument. The viewer for the documentation functions like the less command (arrows to move up/down and exit by pressing ‘q’). It will give a description of what the command does and explain any options it has:

~ $ man rm # Shows help on rm command
~ $ man ls # Shows help on ls command

More on ‘ls’

The ‘ls’ command also has some options. Two useful ones are ‘l’ and ‘h’, ‘l’ shows extra details on the files in a directory, including when the files were last modified and how large they are. The option ‘h’ (human readable) causes ls to show the file sizes in kilobytes (K), megabytes (M) and gigabytes, as opposed to in bytes.

Doing more

The shell can do more than just execute commands, it can help with their execution by skipping commands that don’t do anything and auto-completing what you are typing.

Chaining commands

There are three ways to do this.

Conditionally and

Each command is separated by && on the same line. The later commands are only executed if the earlier ones finished without any errors.

~ $ cd Douments && less notes.txt # less isn't run, as Douments doesn't exist
cd: no such file of directory: Douments

Unconditionally

Each command runs regardless of whether the previous ones succeeded and is separated by ;.

~ $ cd Douments && less notes.txt
cd: no such file of directory: Douments
notes.txt: No such file or directory

Conditionally or

The later commands only run if all the previous ones failed and are separated by ||.

~ $ cd Douments || less notes.txt # less is run, as Documents doesn't exist
cd: no such file of directory: Douments
notes.txt: No such file or directory

For example (the “Dots” directory doesn’t exist):

~ $ cd Dots || cd Documents && less notes.txt # less is run
cd: no such file of directory: Dots

[contents of Documents/notes.txt displayed]

As “Dots” doesn’t exist, the CWD is changed to Documents and notes.txt is printed, if Dots did exist:

~ $ mkdir Dots~ $ touch Dots/notes.txt~ $ cd Dots || cd Documents && less notes.txt # less is run

[empty Dots/notes.txt is displayed]

The second CWD change is skipped. If neither directories existed then the “cd” commands would fail with errors and “less” would not be run.

Autocompletion

When typing a command you can often press the [TAB] key to auto-complete. If the command line can’t figure out what you mean it won’t do anything, but pressing [TAB] twice will list what it thinks you could mean:

~ $ cd Do[TAB] #Nothing, could be the start of "Downloads" or "Documents"
~ $ cd Doc[TAB] #Now it autocompletes for Documents
~ $ cd P[TAB] #Autocompletes, giving:
~ $ cd Pictures
#Commands can also be completed
~ $ tou[TAB] Doc[TAB]notes2.txt # This expands to:
~ $ touch Documents/notes2.txt

Wildcards

Sometimes you might want to select all the files ending with “.jpg” or those starting with “note” and ending with “.txt”. To do this place an ‘*’ where the unknown characters go:

~ $ cat Documents/notes*.txt #cat prints the contents of a file, prints notes.txt and notes2.txt
~ $ mkdir Pictures/unsorted
~ $ mv Pictures/*.jpg Pictures/unsorted #Moves all JPG images in Pictures to Pictures/unsorted

A ? will only be swapped with exactly 1 character, unlike ‘*’ which will be swapped with any number of characters:

~ $ mkdir Notes

[add files to Notes, skipped for brevity]

~ $ ls Notes/note*.txt
notes01.txt notes2.txt notes3.txt notes5.txt … notes10.txt notes11.txt
~ $ cat Notes/notes??.txt #Selects notes01.txt, notes10.txt and notes11.txt# notes2.txt-notes9.txt are rejected (as they don't have 2 characters to swap).

Sending a commands output to a file

If you add an argument starting with “>”, followed by the name of a file to replace (if it already exists) and save the output in it, as a plain text file.

For example (use man to learn about find) to locate and save the locations of all notes:

~ $ find ./ -name "notes*.txt" >note_places.txt
# List of filenames matching notes*.txt saved to ~/note_places.txt
~ $ cat Notes/notes*.txt >notes.txt
#All Notes/notes in placed in one file, ~/notes.txt

To add to the end of a file instead of replacing it use “>>” instead of just “>”.

Summary

The shell is powerful, with numerous commands available to do almost anything under the sun.

This was a quick overview. Others which are useful include tar, zip, mplayer, vim, find, locate, du and dmseg. Use the man command or a search the web to find out more.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.