Linux+: Linux Shell 03 – Directories and Files

Discussion in 'Shell / Command Line' started by Jarret W. Buse, Jun 30, 2014.

  1. Jarret W. Buse

    Jarret W. Buse Active Member Staff Writer

    Messages:
    139
    Likes Received:
    169
    Trophy Points:
    43
    Linux+: Linux Shell 03 – Directories and Files

    Everyone dealing with an Operating System (OS) has some familiarity with directories and files. Directories are similar to folders and files are similar to papers. Each folder, or directory, can contain other folders as well as paper, or files. Each file system has a limit to the number of files which a directory can contain.

    NOTE: Perform a search on Linux.org for your specific file system to determine its limitations.

    A directory is technically a file which contains the list of other directories and files within it.

    A file is a single entity of information which is given a name. The files can be an image, document, application data or many other types. There are basically five types of files:

    1. Directories – a file which contains the names of other files which are in the directory
    2. Executables – file containing instructions to perform specific tasks for the user, otherwise known as applications or scripts
    3. Resources – files which represent system resources such as drives, CD-ROM, etc.
    4. System – files which the OS uses to track user information
    5. User – files which contain data created by the user
    Placement of directories and files create a path name. The path names show the location of a file or where a file should be located.

    The Linux system starts at the Root (/). The Root is the starting point for path names. For example, the home directory is located in the Root and is signified as:


    /home/

    Now, within the home folder is a folder for each user who logs onto the system. For example, mine is jarret and the full path is:

    /home/jarret/

    NOTE: A full path starts at the Root and continues to a specific directory or file. Each directory is separated from another by a forward slash (/). The full path can sometimes be referred to as the absolute path.

    It should be noted that a user's home folder, noted by their name, can also be shortened to the tilde (~). Let's assume I have a directory called articles in my home folder. The path to the 'articles' directory can be one of the following:

    1. /home/jarret/articles/
    2. ~/articles/

    NOTE: The last forward slash (/) is used to show that the last name is a directory and not a file. For example, /home/jarret/articles/ is a directory while /home/jarret/articles refers to a file name 'articles'.

    In most programs, to save a file, the full path is given beginning with the Root (/) and continues to final directory. The last entry in the path is the file name. For example, to give a filename to a program to open a file called Test-Article-1.doc in a directory called articles in my home folder would be:

    /home/jarret/articles/Test-Articles-1.doc

    NOTE: Be aware that directory and filenames are case sensitive. Be very careful on this point.

    If you are within a directory and you want to refer back to a previous directory, you can use relative path names. For example, let's say you are in the directory /home/jarret/articles/ and you want to refer to the following file: /home/jarret/games/Doom.txt. From the existing location, you can refer to the file as: jarret/games/Doom.txt. Notice the first character in the path is not a slash (/). In this case we do not start at the Root, but start a previous directory named jarret. Then, instead of going to the articles directory we go to the games directory.

    NOTE: When the first character is not a slash (/) we are referring to a relative directory.

    If we are in a terminal and located at a specific directory and wish to execute a specific application within the directory, we use a ./ before the program name. For example, if I am in the directory /usr/bin/ and want to start my leafpad program, I would simply type in the terminal ./leafpad.

    NOTE: In this instance, I would not need the ./ since the /usr/bin directory is part of the path environment (see Linux+: Linux Shell 2 – Environment Variables.

    Let's say in a terminal you wanted to list all the files in a specific directory, you would use the 'ls' command once you are in the required directory. To change directories, you use the 'cd' command. To change to the Root from where you are currently located, you would do the following: 'cd /'. This means change directory to the Root. To change to the home folder the command would be 'cd /home' or cd /home/' Since we are changing the directory, it is assumed all names listed are directories.

    Once in a directory, we can perform special searches using wildcards. There are two wildcards as follows:

    1. * - represents any number of various characters
    2. ? - represents a single character of any type

    If we wanted to list all files and directories which started with a j, we could type 'ls j*'. The 'j*' means any directory or file name starting with a j and having any number of characters after it. If I wanted all directories and files starting with a j and having only one other character, I would type: 'ls j?'.

    Attached Files:

    • slide.jpg
      slide.jpg
      File size:
      49.4 KB
      Views:
      121,055
    Hambo and DevynCJohnson like this.

Share This Page