Linux+: Linux Shell 04 – Shell Commands Part 1

Discussion in 'Shell / Command Line' started by Jarret W. Buse, Jul 6, 2014.

  1. Jarret W. Buse

    Jarret W. Buse Active Member Staff Writer

    Messages:
    162
    Likes Received:
    233
    Trophy Points:
    43
    Linux+: Linux Shell 04 – Shell Commands Part 1

    Learning your way around a shell is very important. In previous articles, I used a few shell commands for examples, now, we can get into more detailed information about these commands.

    The first one to cover is the 'Change Directory' (cd) command. The cd command is a basic command with no optional parameters to remember. The basic layout of the command is as follows:

    cd path

    NOTE: Be aware that there is a space after the cd command and before the path. For anyone who has used DOS, this will be a tricky thing to remember.

    No matter the current path, you can move to another path by specifying the full path from the Root and starting the path with a forward slash (/). For example, if you were currently in your Home folder and wished to switch to your second partition the command would be:

    cd /media/sda2/

    This is a very basic change of directory from one path to another.

    NOTE: The media folder is the parent of sda2, and sda2 is the child directory of media.

    Let us assume that we want to go to the 'usr' folder and then to the 'share' directory and finally to the 'apt' folder. No matter our location on the drive, we can type the following:

    cd /usr/share/apt

    When we change directories, the final forward slash (/) is not required since the names given must be directories and not folders. If a file name is given which is is not a directory name, then an error should appear that the given path is not a directory.

    If I am currently in the 'usr' folder and wish to change to the share directory, I can type:

    cd share

    In this case, I want to go into a directory of the current folder. I only need to add an initial slash if the directory is from the Root. For example, to go to 'usr' from anywhere, I type:

    cd /usr

    Let us assume the following directory structure exists:

    /home/jarret/articles/Linux/done
    /home/jarret/articles/Linux/working


    If I am in the done folder and wish to change to the working directory, I simply type the following:

    cd Linux/working

    NOTE: Directory names are case sensitive.

    Now, for some of the special commands.

    To change to the Root you type:

    cd /

    If you wish to go to your home folder, you type:

    cd
    cd ~


    To go back a directory, you type:

    cd ..

    For example, if you are in the /usr/share/ folder and you wish to go back to the 'usr' directory, you would type the 'cd ..'. If you wanted to go back two directories, you would type 'cd ../..'. Every '..' separated by a slash takes you back one directory.

    If you have just switched from the /usr/share/ folder to the /home/jarret/ directory and wish to go back, type:

    cd -

    The 'cd -' takes you back to the previous directory you were working in before changing the directory.

    The cd command also supports wildcards. The '*' is used to represent one or more characters and the '?' stands for a single character.

    If I wanted to switch to /home/jarret/articles/ I could type:

    cd /home/jarret/art*

    NOTE: This works if only one folder starts with 'art'.

    If I have a directory named 'articles' and 'articles in progress', I can use the following to change to articles:


    cd article?

    The command works simply because the folder 'articles' has only one character left and it fits the command better. If I perform the following command from the same directory as before, I would get the same result:

    cd article*

    The '*' can represent a single character as well and since 'articles' comes before 'articles in progress', I end up switching to 'articles'.

    You can also use the TAB key to finish entering the names of directories for you.

    NOTE: The TAB key also works for filename as well.

    If you are in the Root and type:

    cd u[TAB]

    The command should be filled in to 'cd usr/', but only if no other directories or files exist which start with a 'u'. Enough characters must be typed so that the folder is distinguishable from other folders.

    The cd command has two parameters. Before we get to these, we need to cover Symbolic Links (Soft Links).

    Soft Links are shortcuts to a file or folder. In the case of the cd command, we are only concerned with the folders.

    To create a Soft Link we must remember some rules:

    • The links can cross over file systems (one partition to another).
    • If the target of the link is moved, the Soft Link is not updated.
    For example, if I have a partition '/media/sda7' which I want to associate as a folder called 'data' I would type the following:

    ln -s /media/sda7/ ./data

    No matter where I am from the Root, I can type 'cd /data/ and go to /media/sda7/'. I can create Soft Links to directories which are very long, so I do not have to type them out all the time.

    To be able to use Soft Links with 'cd', we use the -L parameter to force the use of Soft Links; this is the default. If we do not want to use Soft Links and require the use of the full path name, we use the -P parameter.

    Attached Files:

    DevynCJohnson likes this.

Share This Page