Navigating the Linux file system can be overwhelming at first. Understanding where certain files are help you move through the file system and get things done efficiently. At times, you may feel stuck with the console. To help you get started, this post will give you a few examples of basic commands as well as quickly explain permissions and their role. Let’s get started with 12 basic commands to navigate your filesystem!
Before we get started, there are a few things to keep in mind:
- Remember that the Linux filesystem is, by default, case sensitive. For example, “FileName” does not equal “filename”.
- A lot of commands on Linux are only going to give you output on an error unless you tell it otherwise. For instance:
`cp thisfile thatfile`.
If it is successful, you will be returned to the command prompt. There will be no “File copied Successfully!” message. You will only see output for an error, such as “File not Found”.
Keep in mind, most commands in Linux will give you a short “help on usage” output when you use: -h, -help, or –help
If you need more information, run:
Man is short for “manual”. This will surely help! It is more than worth the time it takes to read.
Linux is deep and expects you to learn it. However, don’t be scared. There are plenty of resources out there to become more proficient at navigating the Linux file system and this is one of them!
Now on to Some Basic Commands
ls is equivalent to the dir command in good old DOS (now called Windows).
Is, by default, doesn’t tell you much. It is frequently used with many command line options. An example of this is Is -al, one of the most commonly used command line options. The -a is show all while -I is for list format. This shows all the information for each file and directory within the directory which was asked for the list.
A typical ls -al command on a Linux root file system (/), might look something like this:
What does that mean?
Let’s start at the top
“Total 95” is the total number of blocks used by the directory. These are about 1KB each. You will only see this when it is called on a directory and may not be noticeable.
For each column on the file/directory listings, we’ll use /bin directory as an example.
Introduction to Permissions
At the heart of “drwxr-xr-x” is “rwxr-xr-x”. These are the permission bits. Permissions consist of 3 sections with 3 values each: user (also called “owner”), group, and others, (aka UUUGGGOOO).
“Rwx” stands for read, write and execute.
- The first set is the user (or “owner”) permissions. (UUU)
- The second set is for the group permissions. (GGG)
- While the last set is for “others”. This means it’s for any other user that is *not* the owner or in the group. They may be referred to as “everyone else”. (OOO)
The “execute” bit on directory kind of stands for “the ability to open”. If you remove “x” on the directories permission, you won’t be able to read, write or do anything to the files inside that directory regardless of the individual file permissions.
You may notice some extra bits on each end of the permissions.
The beginning is usually the “file type” and the most commonly encountered values are “-”, and “d”.
- “-” is “just” a file
- “d” is a directory
You may also encounter “l”, and maybe “s” here.
- “l” is for link, usually a symbolic link
- “s” is for a socket
These are just some of the most common examples. Of course, there are several more possibilities.
At the end of the permissions is the “sticky bit”, or sometimes called the “special bit”. The most common you’ll encounter is “t” on the “/tmp” directory. In the case of the /tmp directory “t” at the end means anyone can read/write to this directory but only the owner can delete it. There are other “sticky bits” too.
You may also encounter an “S” or “s” in the owner and/or group (hopefully not, it’s rather rare these days). Those are for the setuid and setgid bits. If you find one, you might want to be concerned unless you know exactly why it’s needed.
Now to the rest of the output from “ls -al”
- “2” this is the number of “links” for that file/directory/filename. This count of the number of “links” varies from operating system to operating system. It can include actual symbolic/hard links (more on that later), internal directories, etc.
Some versions of “ls” (and certain command line flags) put the size of the directory here instead.
- “Root root” is the user (or owner) and group, respectively, of the file/directory/filename.
These are also very important fields to pay attention to, the user (or owner) and group values go along with the permissions above to determine whether or not there is read or write access to something. If it all doesn’t jive, you’re going to have errors like “can’t write to log file”, “cannot create directory”, “file not found”, etc. This can make permissions a pain.
- “4096” is really the size of the everything in the directory, or the size of the file. In this example, it is showing bytes, but you can get it to display in a more “human readable” format by using the “-h” command line option.
- “Sep 18 19:20” is, as it looks like, the date stamp.This usually shows the last *modification* time of the file/directory/.
Finally, the last field is the actual name of the file/directory/filename.
Now, you may be wondering how to change those permissions and the user and group. Don’t worry, we’ll get to that in just a minute!
cp stands for copy. For example:
`cp /home/user/file /home/user/newfile`.
mv: move, similar to copy but moves the file, and removes the original. For example,
`mv /home/usr/file /home/user/newfile`
cd: change directory. You’ll need this to move around the file system.
cd takes you up one directory, and you can use that “in line” to move around faster, or do seemingly magical things when you are not sure of the directory name.
A few examples are:
mkdir: Create a new directory
Another command that can come in handy is mkdir -p. This specifies all the directories in the path if it needs to.
mkdir -p /home/user/mySuperCoolProject/source
The latter will create both “mySuperCoolProject” and “source” in the /home/user directory with a single command.
rm: remove. aka: delete
Delete a file, to delete a directory and everything in it, use:
To force delete:
Be careful with that option! Otherwise, you can delete your entire file system as root with one basic command.
df: Disk Free
Show the usage of the filesystems.
A more common use may be:
“-h” is for “human readable” output.
du: This at times may be called “Disk Usage”. It will show you how much disk a file or directory is using.
A good example is:
`du -ch /home/user`
9. ln -s
ln -s <target> <link name>: Create a “Symbolic Link”.
Symbolic links are small files that link to another object. Think of these as a “Shortcut” in Windows — only way way better. Or an alias in Mac.
Need a file in 2 places at once? Have an often edited config file and you are tired of typing out the long path to edit it? You need a symlink! Just be aware of the permissions on the target.
file: allows you to determine and find out the extension of a file. This can range from an image, zip file, mp3, and more!
An example of when you can use it is if someone sends you a file and you are not quite sure what it is, or if you forgot to use an extension on a file.
chown: stands for change owner/group of files and directories.
As a simple example:
chown user:usergroup /home/user/file
You probably won’t need this too much. It may be needed when manually moving files and navigating through the Linux file system during manual upgrades. Also, at times you want to “lock” a file so only root can edit it, but it’s owned by your user. Chown is a quick way to do that, since you created it.
chmod: allows you to change file and directories, read, write, and execute permission bits and other special permissions. The “-rwxr-x-rx” part!
Common usage is to specify what you want, which can sometimes be tedious. It is important to be very careful with permissions as you do not want to lock yourself out of something, or open up the wrong file to the entire world.
One simple example is, let’s say you want to create a directory in your users home directory that is served by http. For security purposes, you don’t want the httpd to be able to write to this directory. You only want httpd to read from it:
`chmod u+rwx,g+rx,o+rx /home/myuser/mysuperCoolWebsite`
will make the “mySuperCoolWebsite” directory permissions to be “drwxr-x-rx”, which is actually the default for newly created directories, files are “-rw-r–r–” by default.
You may also see ‘chmod’ examples that use numbers instead. This is called Octal Notation. This uses 3 numbers to represent the “rwx” bits for user, group, other.
- 1 = x
- 2 = w
- 4 = r
With Octal Notation, you add the numbers up for what you want. rwx = 7, r-x = 5
You can also do things such as:
‘chmod 744 /home/user/file’
The permissions on “/home/user/file” will be rwxr–r–.
Now you’re ready to dive into the Linux Terminal!
Navigating the Linux file system may be a learning curve, but getting in the habit of using the basic commands will make it much easier. As you become more familiar with the Linux terminal, take note of your permissions and their role. This is important as you make some changes. The 12 basic commands you learned above will also help you become more comfortable locating files while making changes.