This article is for the DIYer or new developer looking to get an understanding of some Linux through the command line. If you are getting into building your own websites or applications, eventually you will have to jump into the dark mysterious world of the terminal. Like a ship lost at sea in a storm you probably feel confused and terrified of the unfamiliar interface in front of you. Don't worry, the command line is easy to get the hang of, but can be unforgiving... there is no "trash" folder and most of us learn that the hard way. This is the first part of a multi-part series aimed at helping DIYers and junior developers get comfortable working in the command line.
Hello World.
First off, let's cover some basic commands to help you find where you are, who you are, and what is around you. Finding out which user you are currently logged in as is pretty easy to remember, you're just asking the terminal a question.
dan@dan-pc:~$ whoami
dan
As easy as asking whoami
and we know we are logged in as dan. Pretty simple, but it comes in handy sometimes when trying to debug or figure out why something is not running correctly.
The next command will show you where you are in relation to the root directory of the system (or /)
dan@dan-pc:~$ pwd
/home/dan
This lets us know our present working directory. Now, let's see what is in our pwd.
dan@dan-pc:~$ ls
Desktop Documents Music Public
The command ls
lists the files and directories in your present working directory. Ok, so this is a good spot to jump into another part of using the command line. Most commands have a wide range of options for running the commands. You can view these by typing the command followed by --help
, for example ls --help
. This will give you the instructions on how to use the command and the arguments and/or options for executing the command. Try it with ls
and you will get a lot of interesting information about what you can do with the command. Another, sometimes more friendly option, is to use the man
command, for example, man ls
, this command will take you to the manual page for that command. It should be pretty similar to the --help
command but may offer additional information or links to find more. With that out of the way let's look at a few arguments we can use with ls
and what they do.
Argument | Description |
-a | Lists files and directories including those starting with a . (hidden files) |
-l | Long list, prints out additional information like size, date last modified, etc. |
-t | Sort by modified time, newest first |
-c | Used with -lt, Sorts and shows ctime (time of last modification) |
Run a few of those and see what you get. I think the most common I use, basically, every time I'm in the command line, are ls -la
and ls -ltc
. There are a lot more options than that so feel free to explore and try some of the other options. Some of these commands, like ls -la
show important information like file permissions, the owner of the file, and the group the file belongs to. This becomes important when troubleshooting web server issues like why is XXXX file not accessible or why is XXXX file not running. In a lot of cases, it will be a permission or owner/group issue. Now you should have a good idea of what user you are (whoami
), where you are (pwd
), and what is around you (ls
). So how do you get out of here and explore somewhere new?
Hitching A Ride Out of PWD
Ok, so now you are super excited and ready to explore a bit. Getting around in the command line is easy, don't let anyone tell you otherwise. There is one command you need to remember to move around and navigate through the directory structure or changing directories.
The only command you need to know for moving around is, you guessed it, cd
. This tells the command line you want to change your present working directory. How do we give it directions?
dan@dan-pc:~$ cd ..
That will move you up one level. That sounds weird, when we say up we mean up the directory tree, which technically starts at root. So you are moving up one directory.
/ (root)
|
|
/home
|
|
|
/dan (PWD)
Imagine it's something like that, cd ..
is saying "move us out of this directory to our parent directory" or move our present working directory to /home. To go from /home/dan
to just /
you can use cd ../..
, this just says move up two directories, cd ../../..
move me three, etc. Another way to do this is to use the absolute path. Say we wanted to get to /var/log
from /home/dan
well that is not in our branch of the directory tree so how do we get there? We would use the absolute path of where we want to go or the path from root to that location,
dan@dan-pc:~$ cd /var/log
dan@dan-pc:/var/log$
You'll then be in the /var/log
directory, and you'll also notice that has been tacked on to your identifying information on the left. Now that you are out of your home the command line tells you where you are. Let's jump back to ls
for a second now that we understand absolute paths.
Go back to your home directory using cd
and then enter the command ls -la /var/log
this command will output the contents of the /var/log
directory, using this command you do not have to move into the directory to get a list of what is in that directory.
Great, now you should be comfortable finding your way around and traversing through the directory, but how do you know where things are and when you get there how do you look at them?
Searching for Answers
You are in your home directory, and want to locate a file or directory in the massive world of the command line, what do you do?! There are a few important ways to find things. First, we'll take a look at locate
.
Locate does exactly that, attempts to locate every directory or file with a given term in its path. Fair warning, this can dump a massive amount of results on you and this is searched in an internal database that updates daily, which means it may not include newly created files and directories. Besides that, it can be incredibly helpful in locating files and directories. Try creating a file in your Documents folder with the name findme.txt
and put a small message in the file, for now just do this with a text editor we'll jump into creating files in the command line a bit later. Once the file is created go back into the command line and execute the locate command.
dan@dan-pc:~$ locate findme.txt
Did you get an error? You may not have locate installed in your system! That's ok follow the on-screen instructions to install locate.
dan@dan-pc:~$ sudo apt install mlocate
Should do the trick, it will ask you for your password and then it will install locate. To be sure it is installed correctly, here is another way to search, use the command whereis locate
to search for it's installed location. This will return its location, source, and man page if available.
dan@dan-pc:~$ whereis locate
locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
Great locate is installed! Now try your search again to see where findme.txt is. Now that you know where that file is you can easily navigate to its directory using cd
or you can display its contents in the command line using the absolute path of the file.
Didn't work? Don't worry, remember the whole internal database thing? Try using the find
command instead. This command is far more powerful than locate
and you should review the man
page. Search for your findme.txt file using the command find /home -name findme.txt
.
The Cat Has a Head and a Tail
The output of your locate or find command should have given you something like /home/dan/Documents/findme.txt
, to view that file in the command line we can do a few things. First up is cat
, this command prints the entire file into the command line, which sometimes ends up being a huge scrolling mess that completely overwhelms you but for now it won't. Try printing the contents of your findme.txt file using cat
and the absolute path to the file or by changing into the files directory and using the file name like cat findme.txt.
. The contents will print directly to the screen, see how easy that is, imagine that file is 5,000 lines long and not so easy.
Two additional options we have are head
and tail
which, respectively, show the starting ten and ending ten lines of a file. The number of lines displayed can be adjusted with the -n
option followed by a number of lines to display. For example, the syslog usually has a lot of entries, but we only want to see the last 25 entries, to do so we would execute tail -n 25 /var/log/syslog
. Say you are running an Apache web server and want to see the first 25 entries of the error log, how would you do that? If you guessed head -n 25 /var/log/apache2/error.log
you would be correct!
If you really need to read through that 5,000 line file the answer is more or less...
No Really, the Answer is more or less
Besides the cat
command we can also use more
or less
to view large files one page at a time. This allows you to use the scroll bar to scroll through the file, use the spacebar to skip pages, etc. These two commands help work through large error logs and files in a managable way and in true comedic fashion less
is more in the sense that less
gives you more options than more
. Additionally, we can use some more advanced techniques by "piping" the cat
command over to grep
. The grep
command says "Hey! Apply this filter to the output."
We won't get too deep into grep
for now, but it is an important tool when you are searching through log files. You use grep by piping |
output into grep
. Piping is just an easy way of saying "redirect the output of this command/process to this command/process." Piping is powerful and you'll use it a lot with tools like grep
or awk
. In our example we'll run cat findme.txt | grep "[SEARCH TERM]"
, replace [SEARCH TERM]
with a word or two from your findme.txt file and run the command. The output should show you the lines with matching strings. Imagine that same 5,000 line file is your server's auth.log file and you are trying to see when a certain IP accessed your server.
Bon Voyage
After working through this you should feel comfortable navigating the stormy seas of the command line, viewing files, and figuring out just exactly who and where you are. Next time we'll jump into file creation and manipulation. Thanks for reading!
Cheatsheet
A quick list of the commands we covered in this post.
whoami
pwd
ls
ls -l
ls -la
ls -ltc
cd ..
cd ../..
cd /var/log
locate
whereis
cat
head
head -n 50 [FILE]
tail [FILE]
tail -n 50 [FILE]
more [FILE]
less [FILE]
cat [FILE] | grep "[TERM]"