Exploring Files in Other Directories
So far, you have been using the ls
command to list files in the current directory. However, ls
can also be used to list files in other directories without changing your current location.
Listing Files in a Specific Directory
You can list the contents of a specific directory by providing its path as an argument to the ls
command:
ls -l ~/project/folder1
This command will list the contents of the folder1
directory. Since we haven't created any files in that directory yet, the output might show:
total 0
Let's create a file in the folder1
directory and then list its contents:
touch ~/project/folder1/example.txt
Now when you run:
ls -l ~/project/folder1
You should see:
total 0
-rw-r--r-- 1 labex labex 0 Aug 15 10:45 example.txt
Listing Multiple Directories
You can also list the contents of multiple directories in a single command:
ls -l ~/project ~/project/folder1
This will display the contents of both directories, with headers indicating which directory is being shown:
/home/labex/project:
total 4
-rw-r--r-- 1 labex labex 0 Aug 15 10:30 file1.txt
-rw-r--r-- 1 labex labex 0 Aug 15 10:30 file2.txt
drwxr-xr-x 2 labex labex 4096 Aug 15 10:45 folder1
-rw-r--r-- 1 labex labex 0 Aug 15 10:30 .hidden_file
/home/labex/project/folder1:
total 0
-rw-r--r-- 1 labex labex 0 Aug 15 10:45 example.txt
Combining Options for Different Directories
You can combine various options discussed earlier to get specific information about files in different directories:
ls -la ~/project/folder1
This command will show all files (including hidden ones) in the folder1
directory with detailed information:
total 8
drwxr-xr-x 2 labex labex 4096 Aug 15 10:45 .
drwxr-xr-x 3 labex labex 4096 Aug 15 10:30 ..
-rw-r--r-- 1 labex labex 0 Aug 15 10:45 example.txt
The ability to list files in different directories without changing your current location is a powerful feature of the ls
command that streamlines file system navigation.