Navigating Directories
In a typical graphical interface (desktop environment), you can browse directories using a mouse, but on the Linux terminal, you need to use various commands to navigate, interact with files, and directories.
This section is designed to teach the basics of Linux navigation, including navigating directories, listing files, managing files, and using shortcuts for efficiency.
Linux File System Structure
The Linux file system is hierarchically organized, starting with the root directory /. Understanding this structure is key to effectively navigating in Linux.
/: The root directory, the base of the file system.
/bin, /sbin: Contains essential user and system binaries.
/etc: Contains system configuration files.
/home: Contains personal directories for users.
/var: Contains variable files like logs and databases.
/usr: Secondary hierarchy for user data; contains most user programs and utilities.
Getting Started with Linux Navigation
First, it's important to know which directory/location you're in. The pwd (print working directory) command will show you your current location in the file system.
user@hackerbox:~$ pwd
/home/userIn the example above, the user is in the /home/user directory. The home folder contains individual home directories for users. Each user typically has a home directory, and the terminal starts in the user's home directory when it is first opened.
To list the files and folders in your current directory, you can use the ls command.
user@hackerbox:~$ ls
Desktop Documents Downloads Music Pictures VideosThe above example shows the output of the ls command. As seen, there are 6 folders in the current location. You can detail the output of the ls command using the -l option, which shows details such as permissions, ownership, and modification times.
user@hackerbox:~$ ls -l
total 8
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Desktop
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Documents
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Downloads
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Pictures
drwxr-xr-x 2 user users 4096 Jul 29 08:24 VideosThe output with the -l option includes columns with the following structure:
drwxr-xr-x
File type and permissions
2
Number of hard links to the file/directory
user
Owner of the file/directory
users
Group owner of the file/directory
4096
Size of the file or blocks used to store the directory information
Jul 29 08:24
Creation or last modification date of the file/directory
Desktop
Name of the file/directory
We have listed the contents of the directory, but there might be hidden files/directories we don't see. In Linux, files and directories starting with . are considered hidden files (e.g., .bashrc file). These won't be listed by default with the ls command. To include hidden files in the list, use the -a option with ls.
user@hackerbox:~$ ls -a
total 12
Desktop Documents Downloads Music Pictures Videos .bashrcAs seen, the previously hidden .bashrc file is now listed in the output of the ls command.
You can combine the -la nd -a options with ls -la.
user@hackerbox:~$ ls -la
total 12
drwxr-xr-x 3 user user 4096 Aug 1 10:00 .
drwxr-xr-x 4 user user 4096 Aug 1 09:58 ..
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Desktop
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Documents
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Downloads
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Pictures
drwxr-xr-x 2 user users 4096 Jul 29 08:24 Videos
-rw------- 1 user users 220 Jul 29 09:58 .bashrcAs seen, combining the -l and -a options allows for both detailed listing and inclusion of hidden files.
Navigating to Other Directories
You don't need to be in a directory to list its contents. You can provide the path to a directory as a parameter to the ls command:
user@hackerbox:~$ ls -l /var/log
total 2097152
drwxr-x--- 2 root root 4096 Aug 1 06:25 apache2To change your current directory, use the cd (change directory) command:
user@hackerbox:~$ cd /tmp
user@hackerbox:/tmp$If you wish to go back to the previous directory, simply type cd -.
user@hackerbox:/tmp$ cd -
/home/userAnother feature you should know about while navigating directories is auto-completion. It speeds up your navigation and prevents typos.
Type
cd /usr/sand press the Tab key twice, which will suggest directories starting with "s" in the /usr/location, allowing you to effortlessly write the path you want to navigate to.
Last updated