shell, navigation

shell, navigation

This series provides an introduction to navigating a Unix system using the shell, with examples of common commands for listing files and directories.

·

17 min read

prerequisites

beginner's friendly

Laptop or phone

Terminal

Internet access

At the end of this blog, readers should be able to make use of the following highlights on the table of content effectively.

Overview

The shell navigation project is a comprehensive introduction to the Linux shell and its commands. This project is aimed at beginners who are interested in learning the fundamentals of the shell and its navigation. The project covers a wide range of topics, including how to navigate the shell, how to manipulate files and directories, how to manage permissions and ownership, and how to run commands with root privileges.

In this tutorial one, we started with an introduction to the shell and its basic navigation commands. We learned how to navigate the file system using commands such as ls, cd, and pwd. We also learned how to create and remove files and directories using commands like mkdir, touch, and rm. Additionally, we covered how to view file contents using cat, less, and head commands.

I have also shared the knowledge that I gained about the shell Unix system, while learning from the ALX software engineering program, I hope this first tutorial on the shell Unix systems tagged as 'shell navigation' will be a good read for those who are interested to learn the Shell Unix systems.

The next tutorial will focus on shell, permission,, and other man pages.

Introduction

A shell is a command-line interface that allows a user to interact with an operating system by executing commands. It is a text-based interface, where users can type commands and receive output from the computer. The shell is a crucial component of any Unix-based system, including Linux and macOS.

The shell allows users to navigate the file system, manipulate files and directories, and run programs. One of the most common shells used in Unix-based systems is the Bash shell (short for Bourne-Again SHell). Bash is a command-line interface that provides users with a powerful set of tools for working with the operating system.

Navigation in the shell involves using commands to move between directories, list the contents of directories, and manipulate files and directories. Some common navigation commands in the Bash shell include:

  • cd: change directory. Allows users to move between directories.

  • ls: list directory contents. Shows the files and directories in the current directory.

  • pwd: print working directory. Displays the current directory.

  • mkdir: make directory. Creates a new directory.

  • rm: remove. Deletes a file or directory.

  • cp: copy. Copies a file or directory.

  • mv: move. Moves a file or directory.

    How to navigate in a Unix system

    Navigating in a Unix system involves using commands to move between directories, list the contents of directories, and manipulate files and directories. Here are some common navigation commands that can be used in a Unix system:

    1. cd: This command is used to change the current working directory. To change to a directory called "Documents", for example, type cd Documents.

    2. ls: This command lists the contents of the current directory. To list the contents of a directory called "Documents", for example, type ls Documents.

    3. pwd: This command shows the present working directory. To display the present working directory, type pwd.

    4. mkdir: This command is used to create a new directory. To create a directory called "test", for example, type mkdir test.

    5. rm: This command is used to remove files or directories. To remove a file called "file.txt", for example, type rm file.txt. To remove a directory called "test", type rm -r test (the -r flag is used to remove directories recursively).

    6. cp: This command is used to copy files or directories. To copy a file called "file.txt" to a directory called "Documents", for example, type cp file.txt Documents. To copy a directory called "test" to a directory called "Documents", type cp -r test Documents.

    7. mv: This command is used to move or rename files or directories. To rename a file called "file.txt" to "newfile.txt", for example, type mv file.txt newfile.txt. To move a file called "file.txt" to a directory called "Documents", type mv file.txt Documents. To move a directory called "test" to a directory called "Documents", type mv test Documents.

How to list files and directories

In Unix-based systems, the command-line interface or terminal can be used to list files and directories. Here are some common commands for listing files and directories:

  1. ls: This command is used to list the files and directories in the current working directory. To use this command, simply type ls in the terminal and press Enter. This will display the names of all files and directories in the current directory.

  2. ls -l: This command is used to list files and directories in the current directory, along with their permissions, size, and creation date. To use this command, type ls -l in the terminal and press Enter.

  3. ls -a: This command is used to list all files and directories, including hidden files and directories (those with a dot at the beginning of their names), in the current directory. To use this command, type ls -a in the terminal and press Enter.

  4. ls -lh: This command is used to list files and directories in a human-readable format, showing their size in a more easily understandable format (e.g., in kilobytes or megabytes). To use this command, type ls -lh in the terminal and press Enter.

  5. ls <directory>: This command is used to list the files and directories in a specific directory. Replace <directory> with the name of the directory you want to list. For example, to list the files and directories in a directory called "Documents", type ls Documents in the terminal and press Enter.

How to display the content of a file

To display the contents of a file in Unix-based systems using the command-line interface, you can use the cat command. Here's how to do it:

  1. Open the terminal and navigate to the directory where the file is located using the cd command.

  2. Once you are in the correct directory, type cat followed by the name of the file you want to display. For example, to display the contents of a file called "file.txt", type cat file.txt in the terminal and press Enter.

  3. If the file is large, the entire contents may scroll off the screen too quickly for you to read. In that case, you can use the more command instead of cat. This will display the file one page at a time. To use the more command, type more followed by the name of the file. For example, to display the contents of a file called "file.txt" one page at a time, type more file.txt in the terminal and press Enter. Press the spacebar to display the next page, or press q to quit.

  4. If you want to search for a specific word or phrase within the file, you can use the grep command followed by the search term. For example, to search for the word "example" within a file called "file.txt", type grep example file.txt in the terminal and press Enter.

How to create a file directory

In Unix-based systems, files and directories can be created using the command-line interface or terminal:

  1. touch: This command is used to create a new file. To create a file called "file.txt", for example, type touch file.txt in the terminal and press Enter.

  2. mkdir: This command is used to create a new directory. To create a directory called "newdir", for example, type mkdir newdir in the terminal and press Enter.

  3. mkdir -p: This command is used to create a new directory and any necessary parent directories that do not already exist. To create a directory called "newdir/subdir", for example, type mkdir -p newdir/subdir in the terminal and press Enter.

  4. echo: This command is used to create a new file and add content to it. To create a file called "file.txt" and add the text "Hello, World!" to it, for example, type echo "Hello, World!" > file.txt in the terminal and press Enter. The > symbol is used to redirect the output of the echo command to a file.

  5. nano: This command is used to create a new file and edit its contents using the Nano text editor. To create and edit a file called "file.txt", for example, type nano file.txt in the terminal and press Enter. This will open the Nano text editor, where you can add and edit the contents of the file.

How to remove a file or directory

In Unix-based systems, files and directories can be removed using the command-line interface or terminal,such as:

  1. rm: This command is used to remove files. To remove a file called "file.txt", for example, type rm file.txt in the terminal and press Enter. Be careful when using this command, as it will permanently delete the file and there is no way to recover it.

  2. rm -r: This command is used to remove directories and their contents recursively. To remove a directory called "mydir", for example, type rm -r mydir in the terminal and press Enter. Be careful when using this command, as it will permanently delete the directory and all of its contents and there is no way to recover them.

  3. rmdir: This command is used to remove empty directories. To remove an empty directory called "mydir", for example, type rmdir mydir in the terminal and press Enter.

  4. rm -f: This command is used to forcefully remove files without prompting for confirmation. To remove a file called "file.txt" without confirmation, for example, type rm -f file.txt in the terminal and press Enter. Be careful when using this command, as it will permanently delete the file and there is no way to recover it.

  5. rm -rf: This command is used to forcefully remove directories and their contents without prompting for confirmation. To remove a directory called "mydir" and all of its contents without confirmation, for example, type rm -rf mydir in the terminal and press Enter. Be careful when using this command, as it will permanently delete the directory and all of its contents and there is no way to recover them.

    How to move or copy a file or directory

    In Unix-based systems, files and directories can be moved or copied using the command-line interface or terminal. Here are some common commands for moving or copying files and directories:

    1. mv: This command is used to move or rename files and directories. To move a file called "file.txt" from the current directory to a directory called "mydir", for example, type mv file.txt mydir/ in the terminal and press Enter. To rename a file called "oldname.txt" to "newname.txt", for example, type mv oldname.txt newname.txt in the terminal and press Enter.

    2. cp: This command is used to copy files and directories. To copy a file called "file.txt" from the current directory to a directory called "mydir", for example, type cp file.txt mydir/ in the terminal and press Enter. To copy a directory called "mydir" and its contents to a directory called "backup", for example, type cp -r mydir backup/ in the terminal and press Enter. The -r option is used to copy directories and their contents recursively.

    3. rsync: This command is used to synchronize the contents of two directories. To synchronize the contents of a directory called "mydir" with a directory called "backup", for example, type rsync -av mydir/ backup/ in the terminal and press Enter. The -a option is used to preserve the permissions, ownership, and timestamps of the files and directories, while the -v option is used to display a verbose output.

conclusion

  1. the shell is a powerful command-line interface that allows users to interact with the underlying operating system of their computer, while Navigation in the shell involves moving between directories and viewing their contents using commands like cd, ls, and pwd. Users can create, copy, move, and delete files and directories using commands like touch, cp, mv, and rm. By mastering these basic navigation and file management commands, users can become more efficient and productive when working with Unix-based systems. Hurray! Lets practice some exercises.

    • Change your working directory to /shell

    • Then, create an empty file intresting_session

Advice:

  1. Don’t forget to validate your current working directory

  2. Don’t forget to display the list of files in your current directory to validate the creation of the new file

  3. Open the terminal.

  1. Type cd /shell and press Enter to change the working directory to /shell. If you get a permission denied error, it means you don't have sufficient privileges to access the root directory.

  2. Type touch interesting_session and press Enter to create an empty file called interesting_session in the current directory.

    Here is an example of what the commands and output may look like in the terminal:

    user@hostname:~$ cd /shell
    bash: cd: /shell: Permission denied
    user@hostname:~$ sudo cd /shell
    [sudo] password for user:
    user@hostname:/home/user$ cd /shell
    shell@hostname:~# touch interesting_session
    shell@hostname:~#
    

    In this example, the user first attempted to change the working directory to /shell but was denied permission. They then used the sudo command to gain administrative privileges and successfully changed the working directory to /shell. They then used the touch command to create an empty file called interesting_session in the /shell directory. The command prompt changed to shell@hostname:~# to indicate that the user now has shell privileges.

    Exercise 2

    Change your working directory to /shell

    Then, create a directory I_want_more_exercises

    to change the working directory to /shell and create a directory called I_want_more_exercises:

  3. Open a terminal window on your computer.

  4. Type the following command to change the working directory to /shell:

     cd /shell
    
  5. Press Enter to execute the command. If you have the necessary permissions to access the /root directory, the command will succeed and your current working directory will be changed to /root.

  6. Next, use the following command to create a new directory called I_want_more_exercises:

     mkdir I_want_more_exercises
    
  7. Press Enter to execute the command. If you have the necessary permissions to create directories in the current working directory, the command will succeed and a new directory called I_want_more_exercises will be created in the /root directory.

  1. To verify that the new directory has been created, you can use the following command to list the contents of the /root directory:

ls -l

  1. Press Enter to execute the command. You should see a list of files and directories in the /shell directory, including the new directory called I_want_more_exercises.

Great! Let's do some more!

solution:

Exercise 1: Create a directory called mydir in your home directory, and then create an empty file called myfile inside mydir.

Solution:

cd ~    # change to the home directory
mkdir mydir    # create a directory called mydir
cd mydir    # change into the mydir directory
touch myfile    # create an empty file called myfile

Exercise 2: Copy the myfile file from mydir to the parent directory of mydir, and then delete the original file.

Solution:

cd mydir    # change into the mydir directory
cp myfile ..    # copy myfile to the parent directory of mydir
rm myfile    # delete the original myfile file in mydir

Exercise 3: List all files and directories in the /usr/bin directory, sorted by file size in a human-readable format.

Solution:

ls -lhS /usr/bin

In this command, the -l option tells ls to list files in long format, which includes additional information such as file permissions, ownership, and file size. The -h option tells ls to display file sizes in a human-readable format (e.g. "1.2K" instead of "1234"). The -S option tells ls to sort files by file size, with the largest files first.

Exercise 4: Display the contents of the /etc/passwd file, and then display only the first 5 lines of the file.

Solution:

cat /etc/passwd    # display the contents of the file
head -n 5 /etc/passwd    # display only the first 5 lines of the file

The first command cat is used to display the contents of the file. The second command head is used to display only the first 5 lines of the file. The -n option is used to specify the number of lines to display.

Advance exercises

Exercise 1: Find all files in the /var/log directory that have been modified in the last 24 hours, and then copy them to a backup directory called log_backup.

on:

find /var/log -type f -mtime -1 -exec cp {} ~/log_backup/ \;

This command find is used to search for files in the /var/log directory that have been modified in the last 24 hours. The -type f option tells find us to search only for files (not directories). The -mtime -1 option tells find to search for files that have been modified within the last 24 hours. The -exec the option is used to execute a command on each file that is found. In this case, the command is, which copies each file to the log_backup directory in the user's home directory.

Exercise 2: Create a compressed archive of all files and directories in the current directory, and then extract the archive to a new directory.

Solution:

tar -czvf archive.tar.gz *    # create a compressed archive of all files and directories in the current directory
mkdir new_directory    # create a new directory for extracting the archive
tar -xzvf archive.tar.gz -C new_directory/    # extract the archive to the new directory

In the first command, tar is used to create a compressed archive of all files and directories in the current directory. The -c option tells tar to create a new archive, the -z the option tells it to compress the archive using gzip, the -v the option tells it to display verbose output (i.e. list all files as they are added to the archive), and the -f the option specifies the filename of the archive. The * argument tells tar to include all files and directories in the current directory.

In the second command, mkdir is used to create a new directory called new_directory. In the third command, tar is used to extract the archive to the new_directory directory. The -x option tells tar to extract files from the archive, the -z the option tells it to decompress the archive using gzip, the -v the option tells it to display verbose output, the -f the option specifies the filename of the archive and the -C the option specifies the target directory for extraction. The / after new_directory is necessary to ensure that the contents of the archive are extracted into the new_directory directory, rather than creating a new directory with the same name as the archive.

Exercise 3: Create a script that searches for all files in a directory tree that match a certain pattern, counts the number of occurrences of a specific word in each file, and prints a summary report with the total count of occurrences for each file.

Solution:

Create a new script file called search_word_count.sh and add the following code:

#!/bin/bash

# Usage: search_word_count.sh <directory> <pattern> <word>

if [ "$#" -ne 3 ]; then
  echo "Usage: $0 <directory> <pattern> <word>"
  exit 1
fi

directory="$1"
pattern="$2"
word="$3"

find "$directory" -type f -name "$pattern" -print0 |
  xargs -0 grep -o -w "$word" |
  awk -F ':' '{filename=$1; count[$1]++} END {for (i in count) print i ": " count[i] " matches"}'

This script takes three arguments: the directory to search in, the file name pattern to match, and the word to search for. It uses the find command to search for files in the specified directory tree that match the given pattern and then uses xargs to pass the list of files to grep. The -o option tells grep it to print only the matching word, and the -w option tells it to match only whole words. The output grep is then passed to awk, which uses the colon character as the field separator to extract the filename from each line of output. It then uses an associative array in awk to count the number of matches for each file, and finally prints a summary report with the total count of matches for each file.

To use the script, make it executable with chmod +x search_word_count.sh, and then run it with the desired arguments, for example:

./search_word_count.sh ~/projects '*.txt' 'hello'

This will search for all files with a .txt extension in the ~/projects directory and count the number of occurrences of the word "hello" in each file, and print a summary report with the total count of matches for each file.

Last Exercise: Create a script that backs up all files in a directory tree that have been modified within the last 24 hours to a compressed archive file.

Solution:

Create a new script file called backup.sh and add the following code:

#!/bin/bash

# Usage: backup.sh <directory> <output_file>

if [ "$#" -ne 2 ]; then
  echo "Usage: $0 <directory> <output_file>"
  exit 1
fi

directory="$1"
output_file="$2"
timestamp=$(date +%Y-%m-%d_%H-%M-%S)

# Create a temporary directory to hold the files to be backed up
temp_dir=$(mktemp -d)

# Copy all files modified within the last 24 hours to the temporary directory
find "$directory" -type f -mtime -1 -print0 | xargs -0 cp -t "$temp_dir"

# Compress the files into a single archive file with a timestamp in the name
tar -czvf "${output_file}_${timestamp}.tar.gz" -C "$temp_dir" .

# Clean up the temporary directory
rm -rf "$temp_dir"

This script takes two arguments: the directory to back up, and the name of the output file (without the .tar.gz extension). It first creates a temporary directory to hold the files to be backed up, using the mktemp command. It then uses the find command to locate all files in the specified directory tree that have been modified within the last 24 hours, and copy them to the temporary directory using xargs and cp. It then uses the tar command to compress the files into a single archive file with a timestamp in the name, and saves it in the current directory. Finally, it cleans up the temporary directory with rm.

To use the script, make it executable with chmod +x backup.sh, and then run it with the desired arguments, for example:

./backup.sh ~/projects mybackup

This will back up all files in the ~/projects directory tree that has been modified within the last 24 hours to a compressed archive file named

mybackup_<timestamp>.tar.gz.

Thank you for reading this blog. I hope you learned something today! If you found this guide helpful, please like, share, and follow me for more blog posts like this in the future.

Let's connect I talk about web development, content creation, open source, career tips, and tech jobs on these platforms.

Linkedin

Twitter