Basic Linux Commands: ls, pwd, cd
Operating systems allow users to enter written commands in order to engage directly with the system, bypassing the mouse and desktop we have become so accustomed to using. In Linux, this is called the command terminal. When you first open the terminal, you'll see a window with an empty black screen, with your username as the prompt and a flashing cursor waiting for your first command:1
Source: support.system76.com
It can be rewarding to use the command line, as it often involves having to look up the right command to use, or to find out more about the practical applications of a command. This learning process helps me develop a better understanding the system, and how the files, software and hardware are structured within it.
Linux command line for beginners
There are many ways to learn command line, and using your search engine is not a bad place to begin! I have tried documenting commands I'd learned (and all their applications) by taking notes, but found this approach unhelpful. I have tried reading Bible-sized Linux books, which felt overwhelming. In the end, an O'Reilly publication titled Linux Pocket Guide is what works best for me; online searches can require some weeding out, and I know the information this book is reliable. It sits on my desk within hand reach as I type this, and I use it often.
Learning command line can feel overwhelming if you are new to it. In a way, you get a strong sense of having a bit more power and control than you do using the GUI, or Graphical User Interface, but that can also make you nervous, as you often don't know what you are doing, and mistakes could potentially damage your system. This is why I highly recommend practising on an older laptop or PC, with a mainstream Debian-based Linux install like Ubuntu. Mistakes won't matter so much and you'll easily find the right help online when you get stuck. Another approach is to start up a virtual machine, if you know how.
Before getting started
Below, I will explain three commands in Linux that are a good and safe place to begin.
Notice that:
- in the examples below, you need to always leave a single empty space between the command and any additions to the command, or specific locations or files
- capital and lower case matters ('Documents' is not the same as 'documents')
- I use
as a placeholder for the specific file or directory name in your own system, such as Documents. Don't enter the brackets themselves! - in Linux, spaces between words in a file or directory name can cause problems, so use single quotation marks, as in 'My films folder' when two or more words are part of one file or folder name
Let's get started!
Three commands for navigating your system
We are familiar with opening folders and subfolders (I'll call these directories from now on) by clicking on paper binder-shaped icons with our mouse pointer. You can do the same with commands.
It helps to imagine your computer is a building with lots of floors, and that you can only be in one place at a time. The following commands can help you move up and down floors, open rooms and see what stuff is inside each room in your system.
ls
- lists information about files in current directory
- for example, if you are in your Documents directory, typing
ls
will list all the documents you have saved there
pwd
- stands for 'print working directory'
- typing this command shows you exactly where you currently are in the file system
- in the output of
pwd
different 'floors' in the building (the directories) are shown by a '/' symbol (as in /home/username/Pictures) - typing
pwd
shows you the available rooms (directories) in the floor above you
cd
- stands for 'change directory'
- use to navigate up and down in your directory hierarchy
cd ..
(notice the single space before the two periods!) moves you one level down, closer to the root directory in the basementcd <directory name>
moves you 'upwards' into that directory, if it is available (for examplecd Documents
)cd /
takes you directly to the lowest level, the basement, the root directory
Remember: the pwd
command can tell you where you are in the hierarchy, and show you which directories are one level up, and the ls
command will list all the files saved in the directory you are currently in.
Example
I want to get to my Downloads directory in order to delete a file there.
Typing pwd
shows:
/home/myusername
I remember that /
stands for the root directory, the basement of my filesystem, as it were, and so the pwd
command has shown me that I am several floors up from root, and, in fact, in my home environment (analogous to clicking on the Home icon in desktop). I can now use the ls
command to see where I can go next:
Typing ls
shows:
Applications
Desktop
Documents
Downloads
Pictures
Music
These are the directories that I'd expect to see in my home directory. If I have any stray files in my home folder, those will show up too. To navigate one floor up into the Downloads directory, I will need to use the cd
command:
cd Downloads
The cursor now shows:
~/Downloads$
Which means I'm in that folder (/home/username/Downloads). I can now use the ls
command again to list all the files in the Downloads folder, so I can see the precise name of the file I want to delete. In a next post, I will cover file management, including how to delete files.
Extra tips:
TAB
The TAB button on your keyboard will autotype the rest of the name of a file or directory, provided it's accessible in the directory you're currently in. You just need to type the first few letters correctly (like 'Doc') and then pressing TAB will autotype 'Documents' if a file or directory with that name is there!
Copy & paste
Copy, cut and paste all work in a terminal window, if you have it open in your graphical operating system. Copy, cut and paste also work across the terminal and any other windows or browsers in your OS. This is helpful when you are following instructions in a web tutorial, for example, as you can just copy strings of commands from the page into your terminal window and execute them there by hitting the return button.2
Problems
There are fewer checks and warnings when you use command line. The terminal might start you off with a general reminder that you can destroy things using these commands, which should be heeded. It is indeed possible to wipe your Linux system with a single command, so don't blindly trust all the advice you get online, as you could be engaging with a prankster or worse. Having said that, I have received generous practical help from some individuals on forums. Perhaps start with published help sites or books and magazines, and keep your discussions on forums public, rather than switching to private messaging.
Conclusion
To someone starting out, this may all sound slow and tedious. Why go through all this when you can achieve the same with your mouse pointer and the folder icons in your graphical user interface? But using these commands quickly becomes second nature, to the point where you may prefer using command line, because it sometimes allows you to see more and do more than you are able to with the GUI.
I am not trying to replace online or printed tutorials with this and future posts; there are many excellent resources for using the command line.
My goal here has been to encourage people who may be interested in trying the command line themselves, but haven't yet started for whatever reason, to just begin. Additionally, I thought it might be helpful to direct beginners towards a limited set of key commands that would have been useful for me to know when I first started. Some of the tutorials and books can overwhelm with options and information.
A final note: I've used Linux commands in my examples, but other operating systems will have their own set of corresponding commands. I remember from my Windows days, for example, that the dir
command does the same thing ls
does in Linux.
Documentation
Linux Pocket Guide, 3rd edition by Daniel J. Barrett, 2016.
An A-Z Index of the Linux command line: bash + utilities
How to Use the DIR Command in Windows (How to Geek)
Understanding the Digital World: What You Need to Know about Computers, the Internet, Privacy, and Security, Second Edition by Brian W. Kernighan, 2021.
-----Discuss on Reddit-----
Subscribe to my blog via email or RSS feed.
Find me on Mastodon and Twitter.
Back to Blog
The terminal itself is a kind of graphical interface or emulation too, but if you have ever installed a Linux system without graphical interface, entering commands in the terminal window works just like a GUI-less system.↩
For some reason, I have to use right-click with my mouse and select 'paste' for this. Typing CTRL-V into the terminal space doesn't work.↩