Skip to main content
LPI Linux Essentials

2.3 Using Directories and Listing Files

By April 17, 2013September 12th, 2022No Comments

Now we will shall delve a little deeper into using directories and listing files. The first point to understand is that within Linux everything is a file; directories are just a special type of file.

Identify File Types

We also have files that point to devices such as USB ports and hard drives. These files are located within the /dev directory. Using the command file, we can determine the file type for a given file or files. In the following example we use the command file to query

  • /dev/sda1
  • /etc
  • /etc/hosts
file /dev/sda1 /etc /etc/hosts

The response should be similar to the output shown in the following graphic showing the files to be a block device, directory and finally, a plain file.


Hidden Files

In Linux, hidden files are denoted by those whose file names begin with a dot. Using the -a option with ls we can list all files including hidden files. To get a little clever with our command and piping we could try this:ls -a | grep ‘^.’

The ls command has the option -a and -A. -a is to list all files and -A almost all. The -A option does not list the current directory or .  nor the parent directory denoted with the ..

  • ls -a : list all files in the current directory
  • | : pipe or redirect the output of ls to the next command, in this case grep
  • grep : used to search text
  • ‘^.’ : ^ lines that start with . means literally a dot. The backslash ensure that grep reads the dot as a dot and removes any special meaning from the dot character may have.

There is no place like $HOME

A users home directory will normally be located at /home/USERNAME. This where they can store their own files including personal login scripts and the bash history (a text file that contains a list of recently entered commands). Typically a user will be taken to their home directory upon logon to a text terminal or remote SSH (Secure Shell) session. The environment variable $HOME will list the path to a users home directory. We can print to the screen the contents of a variable using the echo command, echo is a shell built-in or a command that is part of bash itself.echo $HOME

Users may return to their home directory using the bash builtin-in cd with no arguments, alternatively using: cd ~ .The tilde (~) becomes more useful though when you want to reference another users home directory, cd ~bob. The above command would take you to Bob’s home directory, assuming there was a user named Bob. Using the command cd ~/Documents would take you to your home directory and the subdirectory Documents.

Previous Directory

Another useful option with cd is the  option. SO the command would be, 

cd -

The – option takes you to your previous directory.  In this way, it becomes very easy to toggle between two directories just using cd – . This is maintained by the variable $OLDPWD. Looking at the following series of commands entered from the users home directory:

cd
cd /usr/share/doc
cd -
echo $OLDPWD

Step by step

  • From the users’ home directory we change to the /usr/share/doc directory using the directories full path

  • We then change back to the previous directory using cd –

  • On moving to a new directory the variable $OLDPWD is populated with the path to the directory that was left so we see the $OLDPWD now points to /usr/share/doc .

    The commands are captured in the screen shot that follows:

The above screen-shot details how useful cd – can be in making use of the variable $OLDPWD Paths.

File Paths

When referencing files (of any type) we can use full path or a path relative to where we are; the simplest form of a relative path is just the file-name itself which we can access when we are in the same directory as that file. The . by itself means this directory and .. the directory above or parent directory

  • .   = This directory
  • ..  = The parent directory to the current directory
  • ../.. = The directory two directories above the current directory.
  • ../etc  = The etc directory is referenced in the parent directory of the current directory
  • If our current directory is /home/user1, we can refer the file in /etc/hosts in two ways:
    • ../../etc/hosts : a relative path
    • /etc/hosts : a full path, the full path will access the file from any directory and will always start with a forward slash, /.

 

Directory Listings using ls

We have already seen that we can use the -a option with ls to display hidden file; you may also know that ls has many more options. Some of these options you may use all of the time others you may never need to use. Options can also be used together, -l provides for a long listing. If you require to see a long listing of all files we can use ls and the options in the following combinations.

ls -l
ls -a
ls -la
ls -al
ls -a -l

The long listing provides details of the permissions, ownership, size and last modified time for the given file. Using the -R option a recursive listing is provided, in other words the contents of subdirectories and their subdirectories are all listed. Common options for ls include:

  • -R : recursive listing
  • -l : long listing
  • -a : include all files including hidden files
  • -r : reverse sort the listing
  • -n : numerically sort on the User ID and the Group ID
  • -h : used with -l and prints the size rounded up to KB, MB, GB etc