Navigating The Linux Command Line

It’s as easy as navigating DOS, if you can remember that far back. There are one or two differences: the commands have different names, those that look similar have slightly different options, and Linux is case sensitive. For example: in DOS, cd.. will take you back one directory whereas the equivalent Linux command is cd ..

Spot the difference? No, Yes? The Linux command has a space between cd and the two dots (..)

Before I go any further I think it’s important I explain a little about the command line and where you might encounter it.

When you switch-on your desktop computer and leave it to load to a point where you may use it, you are usually greeted with a graphical screen that has a background image, task bar and a menu bar. This is called your desktop.

To tell your computer to load a program you usually use a mouse to move a cursor around the screen until it hovers over the program shortcut then you use the mouse’s left-hand button to “click” the program’s shortcut.

To find data on your computer you would usually use a graphical file browser to help you see and interact with that data.

The desktop with its file mouse and cursor, menu and file browser etc.., is a type of interface called a Graphical User Interface or GUI for short.

It hasn’t always been this way. Before graphical user interfaces, there were text commands. People would stare at a black screen with a flashing white block (also called a cursor). Text commands would be used to load programs, locate data and compute answers.

Finding data and programs stored on a computer can take a long time when you don’t know where to look or even whether it’s on there. Thankfully we now use GUIs. But sometimes we have little choice but to use the command line to navigate a computer and perform tasks. It might be that your computer’s graphical interface is unable to load, or a software upgrade can’t be performed while the GUI is active, or you need to perform multiple operations on a data file and the quickest way to do them is with a command line script.

Here’s an example of how the command line looks as viewed through a terminal from the desktop:

Example of the Linux command line

It looks pretty much like any normal DOS screen. The difference between a command line in a terminal within a GUI and a command line outside of a graphical interface is that there would be just a black screen with the writing “pro@Appropriatarian:~$ ” displayed on it. No menu bar, no fancy graphics.

Looking at the above image, the writing before the @ is the username of whoever or whatever is logged in to the computer; and the bit after the @ is the title of the computer. The bit after the $ (where the gray block cursor is) is called the command prompt.

Here’s another example:

Second Example of the Linux command line

This time there’s more writing. That’s because I’ve changed the directory from the root directory to a sub-directory called WorkHouse.

The root directory is the top of the list of files and data (you could think of it as the folder that every other folder and bit of data is contained within). It is, oxymoronically, the top of the directory tree. The directory tree is the list of files and data from which all directories and sub-directories branch.

Here are a few examples of the directory structure:

dion@journalxtra:~$
dion@journalxtra:~/Documents$
dion@journalxtra:~/Documents/WorkHouse$
dion@journalxtra:~/Documents/WorkHouse/Movies$

The first example, dion@journalxtra:~$, is the root directory. All the others are examples of sub-directories.

File and folder types are denoted by different colors: blue indicates a folder, green indicates an executable (program), red indicates a compressed file, and purple indicates a graphic (.GIF, .JPG, etc.). Other colours are used to denote more file types. These colours can be personalised and you can even set a specific colour to a specific string (a string is combination of characters e.g apple, 12345, 20people, or n1qt#).

The most useful command line navigation instructions enable you to list files, search for files and to move up and down the directory tree.

Remember to pay attention to whether a file’s or folder’s name is written in upper case, lower case or mixed case characters. It makes a difference as Linux is case sensitive.

Those commands are:

find

whereis

ls

cd

clear

Another useful command is history which displays the list of commands used within the Termainal by the active user.

Here’s what they do:

find will search for a file or folder in a specified location.

Examples:

find / -name ‘afile.txt’

would search for afile.txt. The search would begin from the root directory (hence the “/“) and continue downward to all sub-directories. In other words, all storage media would be searched.

find -name ‘afile.txt’

would search for afile.txt. The search would begin from the current directory and continue downwards through all sub-directories. This search would not cover all storage media.

find home/me/Documents -name ‘afile.txt’

would return the path to afile.txt that is stored under home/me/Documents. The search would be restricted to home/me/Documents and its sub-directories.

find -name ‘anyfile*’

Would search for any file or folder that has a title beginning with anyfile. It would return anyfile123, anyfile345 or anyfileABCD. Notice the wildcard “*“, it’s a symbol that represents any character or combination of characters that are directly adjacent to the string to which it is juxtaposed. Using it tells the computer to return any string that matches the characters surrounding it but not to worry about matching the characters between them e.g abcd*1234 would return abcdefgh1234, abcd56781234

find -name ‘*’ -size +1000k

Would search for any file that is larger then 1000k.

find . -size +100000 -print

Would find any file that is larger than 100MB.

whereis locates binary, source and manual page files for a command.

Example:

whereis firefox will list the directories that contain the executable (binary) file called firefox.

ls lists the files and folders in the current directory

Examples:

ls

Would list all the files and folders in the current directory except the hidden files and folders.

ls -a

Would list all the files and folders in the current directory including the hidden files and folders.

ls -1

Would list the files and folders as a single column list and not as the default row format.

ls -l

Would list the files and folders along with their permissions, owners, size and last modification date.

Those options may be combined e.g ls -a -l or ls -a -1

ls can be used to search for files within a folder by specifying the name or initial characters a name of a file to be located then pressing the tab key instead of the return key. For example in the /bin directory ls c followed by the tab key (not the return key) will list all files that begin with the letter c. The search can be narrowed by specifying more characters e.g ls ch. If no files begin with the specified character/s then no results are returned and nothing, absolutely nothing, at all happens – the cursor just blinks, and blinks and blinks until you realise that nothing as happened because there are no files beginning with the specified character.

Other options exist but are beyond the scope of this article.

If the directory list is too big to display in a single screen you can (as with all other commands) use the less instruction to make it scrollable. However, doing so removes the color coding of the files e.g ls -a -l | less. To break out of the scrolling list and remain able to view the current portion of the list, type “q” to quit.

That straight vertical line character “|” before the less command is called a pipe. When you see or hear a statement start with the words “pipe it through…” it means you should type your command then use a pipe “|” after it yet before whatever you’re being asked to pipe it through.

cd means “change directory”. It’s the command used to move up and down the directory listings.

To enter a directory type cd /name/of/directory

To go back one directory type cd .. or to go back two directories type cd ../../

For example:

cd /home/user/Documents

Would navigate to the Documents folder.

Were you at the Documents folder and wanting to return to /home/user then you would type cd ..

To return to the root directory from any other directory you would simply type cd or cd /

Be careful to include the space after cd e.g cd(space).. not cd..

Clear is used to clear the screen of all output.

Have a go. Open a terminal and try the ls command then use cd and cd .. to navigate through the folders and files displayed by the ls command. Use clear whenever you need to refresh the screen.

Leave a Reply