Stay on top of the latest thoughts, strategies and insights from enterprising peers. Show
Learn to navigate from directory to directory in the Linux terminal.
To navigate through the directories of your computer in a graphical interface, you're probably used to opening a window to get "into" your computer, and then double-clicking on a folder, and then on a subfolder, and so on. You may also use arrow buttons or keys to back track.
To navigate through your computer in the terminal, you use the cd command. You can use cd .. to move one directory back, or cd ./path/to/another/folder to jump through many folders into a specific location.The concept of a URL, which you use on the Internet already, is actually pulled directly from POSIX. When you navigate to a specific page on some website, like http://www.example.com/tutorials/lesson2.html, you are actually changing directory to /var/www/imaginarysite/tutorials/ and opening a file called lesson2.html. Of course, you open it in a web browser, which interprets all that weird-looking HTML code into pretty text and pictures. But the idea is exactly the same. If you think of your computer as the Internet (or the Internet as a computer, more appropriately), then you can understand how to wander through your folders and files. If you start out in your user folder (your home, or ~ for short) then everywhere you want to go is relative to that: $ cd ~/Documents $ cd .. $ pwd /home/tux This requires some practise, but after a while it becomes far faster than opening and closing windows, clicking on back buttons and folder icons. Auto-completion with TabThe Tab key on your keyboard auto-completes names of directories and files you're starting to type. If you're going to cd into ~/Documents, then all you need to type is cd ~/Doc and then press Tab. Your shell auto-completes uments. This isn't just a pleasant convenience, it's also a way to prevent error. If you're pressing Tab and nothing's being auto-completed, then probably the file or directory you think is in a location isn't actually there. Even experienced Linux users try to change directory to a place that doesn't exist in their current location, so use pwd and ls often to confirm you are where you think you are, and that your current directory actually contains the files you think it contains.
Subscribe to our weekly newsletter
Privacy Statement
Why use the terminal?"Under Linux there are GUIs (graphical user interfaces), where you can point and click and drag, and hopefully get work done without first reading lots of documentation. The traditional Unix environment is a CLI (command line interface), where you type commands to tell the computer what to do. That is faster and more powerful, but requires finding out what the commands are." This page gives an introduction to using the command-line interface terminal, from now on abbreviated to the terminal. There are many varieties of Linux, but almost all of them use similar commands that can be entered from the terminal. There are also many graphical user interfaces (GUIs), but each of them works differently and there is little standardization between them. Experienced users who work with many different Linux distributions therefore find it easier to learn commands that can be used in all varieties of Ubuntu and, indeed, in other Linux distributions as well. For the novice, commands can appear daunting: sudo gobbledegook blah_blah -w -t -h --long-switch aWkward/ComBinationOf/mixedCase/underscores_strokes/and.dotsHowever, it is important to note that even experienced users often cut and paste commands (from a guide or manual) into the terminal; they do not memorize them. It is important, of course, to know how to use the terminal - and anyone who can manage typing, backspacing, and cutting and pasting will be able to use the terminal (it is not more difficult than that). Starting a terminalIn UnityUnity is the default desktop environment used as of 11.04. Where systems are not ready for Unity they revert to GNOME which is also used in previous releases such as Ubuntu 10.04 LTS (Lucid), see next sub-section. The easiest way to open the terminal is to use the 'search' function on the dash. Or you can click on the 'More Apps' button, click on the 'See more results' by the installed section, and find it in that list of applications. A third way, available after you click on the 'More Apps' button, is to go to the search bar, and see that the far right end of it says 'All Applications'. You then click on that, and you'll see the full list. Then you can go to Accessories -> Terminal after that. So, the methods in Unity are: Dash -> Search for Terminal Dash -> More Apps -> 'See More Results' -> Terminal Dash -> More Apps -> Accessories -> Terminal Keyboard Shortcut: Ctrl + Alt + T In GNOMEGNOME is the classic desktop environment for Ubuntu 11.04 (Natty) and is the default desktop environment in earlier releases, such as Ubuntu 10.04 LTS (Lucid). Applications menu -> Accessories -> Terminal. Keyboard Shortcut: Ctrl + Alt + T In Xfce (Xubuntu)Applications menu -> System -> Terminal. Keyboard Shortcut: Super + T Keyboard Shortcut: Ctrl + Alt + T In KDE (Kubuntu)KMenu -> System -> Terminal Program (Konsole). In LXDE (Lubuntu)Menu -> Accessories -> LXTerminal. Keyboard Shortcut: Ctrl + Alt + T Commandssudo: Executing Commands with Administrative PrivilegesThe sudo command executes a command with administrative privileges (root-user administrative level), which is necessary, for example, when working with directories or files not owned by your user account. When using sudo you will be prompted for your password. Only users with administrative privileges are allowed to use sudo. Be careful when executing commands with administrative privileges - you might damage your system! You should never use normal sudo to start graphical applications with administrative privileges. Please see RootSudo for more information on using sudo correctly. File & Directory Commands
Here is an example of when it would be necessary to execute a command with administrative privileges. Let's suppose that another user has accidentally moved one of your documents from your Documents directory to the root directory. Normally, to move the document back, you would type mv /mydoc.odt ~/Documents/mydoc.odt, but by default you are not allowed to modify files outside your home directory. To get around this, you would type sudo mv /mydoc.odt ~/Documents/mydoc.odt. This will successfully move the document back to its correct location, provided that you have administrative privileges. Running a File Within a DirectorySo you've decided to run a file using the command-line? Well... there's a command for that too! ./filename.extension After navigating to the file's directory, this command will enable any Ubuntu user to run files compiled via GCC or any other programming language. Although the example above indicates a file name extension, please notice that, differently from some other operating systems, Ubuntu (and other Linux-based systems) do not care about file extensions (they can be anything, or nothing). Keep in mind that the 'extension' will vary depending upon the language the source code is written in. Also, it is not possible, for compiled languages (like C and C++) to run the source code directly -- the file must be compiled first, which means it will be translated from a human-readable programming language to something the computer can understand. Some possible extensions: ".c" for C source, ".cpp" for C++, ".rb" for Ruby, ".py" for Python, etc. Also, remember that (in the case of interpreted languages like Ruby & Python) you must have a version of that language installed on Ubuntu before trying to run files written with it. Finally, the file will only be executed if the file permissions are correct -- please see the FilePermissions help page for details. df: The df command displays filesystem disk space usage for all mounted partitions. "df -h" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable") du: The du command displays the disk usage for a directory. It can either display the space used for all subdirectories or the total for the directory you run it on. Example: user@users-desktop:~$ du /media/floppy 1032 /media/floppy/files 1036 /media/floppy/ user@users-desktop:~$ du -sh /media/floppy 1.1M /media/floppy/In the above example -s means "Summary" and -h means "Human Readable". free: The free command displays the amount of free and used memory in the system. "free -m" will give the information using megabytes, which is probably most useful for current computers. top: The top ('table of processes') command displays information on your Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit top, press "q". uname -a: The uname command with the -a option prints all system information, including machine name, kernel name & version, and a few other details. Most useful for checking which kernel you're using. lsb_release -a: The lsb_release command with the -a option prints version information for the Linux release you're running, for example: user@computer:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiricip addr reports on your system's network interfaces. Adding A New UserThe "adduser newuser" command will create a new general user called "newuser" on your system, and to assign a password for the newuser account use "passwd newuser". OptionsThe default behaviour for a command may usually be modified by adding a --option to the command. The ls command for example has an -s option so that "ls -s" will include file sizes in the listing. There is also a -h option to get those sizes in a "human readable" format. Options can be grouped in clusters so "ls -sh" is exactly the same command as "ls -s -h". Most options have a long version, prefixed with two dashes instead of one, so even "ls --size --human-readable" is the same command. "Man" and getting helpman command, info command and command --help are the most important tools at the command line.Nearly every command and application in Linux will have a man (manual) file, so finding them is as simple as typing "man "command"" to bring up a longer manual entry for the specified command. For example, "man mv" will bring up the mv (move) manual. Move up and down the man file with the arrow keys, and quit back to the command prompt with "q". "man man" will bring up the manual entry for the man command, which is a good place to start! "man intro" is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line. There are also info pages, which are generally more in-depth than man pages. Try "info info" for the introduction to info pages. Some software developers prefer info to man (for instance, GNU developers), so if you find a very widely used command or app that doesn't have a man page, it's worth checking for an info page. Virtually all commands understand the -h (or --help) option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Try "man -h" or "man --help" to see this in action. Caveat: It's possible (but rare) that a program doesn't understand the -h option to mean help. For this reason, check for a man or info page first, and try the long option --help before -h. Searching the manual pagesIf you aren't sure which command or application you need to use, you can try searching the manual pages. Each manual page has a name and a short description. To search the names for <string> enter: whatis -r <string>For example, whatis -r cpy will list manual pages whose names contain cpy. The output from whatis -r cpy will in part depend on your system - but might be as follows: memccpy (3) - copy memory area memcpy (3) - copy memory area mempcpy (3) - copy memory area [some lines removed] wcsncpy (3) - copy a fixed-size string of wide characters wmemcpy (3) - copy an array of wide-characters wmempcpy (3) - copy memory area To search the names or descriptions for <string> enter: apropos -r <string>For example, apropos -r "copy files" will list manual pages whose names or descriptions contain copy files. The output from apropos -r "copy files" will in part depend on your system - but might be as follows: cp (1) - copy files and directories cpio (1) - copy files to and from archives gvfs-copy (1) - Copy files gvfs-move (1) - Copy files install (1) - copy files and set attributes Other Useful ThingsPrettier Manual PagesUsers who have Konqueror installed will be pleased to find they can read and search man pages in a web browser context, prettified with their chosen desktop fonts and a little colour, by visiting man:/command in Konqueror's address bar. Some people might find this lightens the load if there's lots of documentation to read/search. Pasting in commandsOften, you will be referred to instructions that require commands to be pasted into the terminal. You might be wondering why the text you've copied from a web page using Ctrl + C won't paste in with ctrl+V. Surely you don't have to type in all those nasty commands and filenames? Relax. ctrl+shift+V pastes into a GNOME terminal; you can also do middle button click on your mouse (both buttons simultaneously on a two-button mouse) or right click and select Paste from the menu. However, if you want to avoid the mouse and yet paste it, use "Shift + Insert", to paste the command. If you have to copy it from another terminal / webpage, you can use "Ctrl + Insert" to copy. Save on typing
!1967 Change the textThe mouse won't work. Use the left/right arrow keys to move around the line. When the cursor is where you want it in the line, typing inserts text - ie it doesn't overtype what's already there.
More ways to run a terminalYou can set your own keyboard shortcut to run a terminal. See KeyboardShortcuts for details of keyboard shortcuts. You can run more than terminal - in tabs or separate windows. You can also install guake (GNOME), tilda (XFCE / LXDE/Mate) or yakuake (KDE) and have a terminal which appears and hides on shortcut key. This can be particularly useful if you use terminal a lot. Drop down terminals can make things a lot easier if you are trying to run a desktop enviroment with a non defualt window manager and something goes wrong drop down terminals can run the orginal window manager --replace to restore a previous option to make things much less painful. An extremely handy tool :: Incremental history searchingIn terminal enter: gedit ~/.inputrc Then copy paste and save: "\e[A": history-search-backward "\e[B": history-search-forward "\e[C": forward-char "\e[D": backward-char From now on, and many agree this is the most useful terminal tool, it saves you a lot of writing/memorizing... All you need to do to find a previous command is to enter say the first two or three letters and upward arrow will take you there quickly: Say I want: for f in *.mid ; do timidity "$f"; done All I need to do is enter: fo And hit upward arrow command will soon appear. How to create upsidedown and/or reverse text with your terminalIf you wish or need to ever flip text upside down [vertical flip] "uʍop ǝpısdn ʇxǝʇ dıʃɟ" or/and create reverse text here is a terminal way to achieve this. Copy/paste and save the following as flip.pl in your home folder (thanks to Lars Noodén for script). #!/usr/bin/perl use strict; use warnings; use utf8; binmode(STDOUT, ":utf8"); my %flipTable = ( "a" => "\x{0250}", "b" => "q", "c" => "\x{0254}", "d" => "p", "e" => "\x{01DD}", "f" => "\x{025F}", "g" => "\x{0183}", "h" => "\x{0265}", "i" => "\x{0131}", "j" => "\x{027E}", "k" => "\x{029E}", "l" => "|", "m" => "\x{026F}", "n" => "u", "o" => "o", "p" => "d", "q" => "b", "r" => "\x{0279}", "s" => "s", "t" => "\x{0287}", "u" => "n", "v" => "\x{028C}", "w" => "\x{028D}", "x" => "x", "y" => "\x{028E}", "z" => "z", "A" => "\x{0250}", "B" => "q", "C" => "\x{0254}", "D" => "p", "E" => "\x{01DD}", "F" => "\x{025F}", "G" => "\x{0183}", "H" => "\x{0265}", "I" => "\x{0131}", "J" => "\x{027E}", "K" => "\x{029E}", "L" => "|", "M" => "\x{026F}", "N" => "u", "O" => "o", "P" => "d", "Q" => "b", "R" => "\x{0279}", "S" => "s", "T" => "\x{0287}", "U" => "n", "V" => "\x{028C}", "W" => "\x{028D}", "X" => "x", "Y" => "\x{028E}", "Z" => "z", "." => "\x{02D9}", "[" => "]", "'" => ",", "," => "'", "(" => ")", "{" => "}", "?" => "\x{00BF}", "!" => "\x{00A1}", "\"" => ",", "<" => ">", "_" => "\x{203E}", ";" => "\x{061B}", "\x{203F}" => "\x{2040}", "\x{2045}" => "\x{2046}", "\x{2234}" => "\x{2235}", "\r" => "\n", " " => " " ); while ( <> ) { my $string = reverse( $_ ); while ($string =~ /(.)/g) { print $flipTable{$1}; } print qq(\n); } Then to set it up: sudo mv flip.pl /bin/ cd /bin/ sudo chown yourusername flip.pl && sudo chmod +x flip.pl Then open terminal and enter: flip.pl else perl /bin/flip.plWrite what you want and hit return Copy and paste wherever you want text document or Internet forum, etc...
================== If you want to reverse back to front, write your text in a text editor and save as mytext to the home folder. Then enter: rev mytext Copy and paste the result, tluser eht etsap dna ypoc. And of course you can combine both for truly cryptic results, ɔodʎ ɐup dɐsʇǝ ʇɥǝ ɹǝsnʃʇ Within the Community Help Wiki: Detailed tutorials on the Linux command line: CategoryCommandLine |