Thursday, March 30, 2023

Linux OS: Basic Terminal Commands for Beginners

Terminal command ls and cat

    For some reason, having the skill to navigate through a Linux system is still essential. Although the GUI is excellent, you won't always have that option on every Linux system. For example, if you need to compress all images in your "Documents/Pictures" directory using jpegoptim and you're currently in the "home" directory, what would you do? You can type the following while you’re still in the home directory:

jpegoptim --size=100k Documents/Pictures/*
Or if you’re using a text-based system, like a server or Termux, and want to see the files and folders in the current directory, you can use the ls command.

Let’s explore more!
 

The Path

Path illustration.

A path represents the location of a file or folder on your computer system. For example, 'home/Dela/Documents' is a path. There are two types of paths, absolute and relative.

1. The absolute path, is the complete or full path to a file or folder. For example: '/home/Dela/Documents/Linux_Book.pdf'.

2. The relative path, begins with a dot (.) to represent the current or working directory. For example, if you’re in '/home/Dela/' and you want to access video.mp4 inside the Movies folder, you don't need to type the full path '/home/Dela/Movies/video.mp4'. Instead, you can simply use the relative path 'Movies/video.mp4' in your terminal.

So here it is:

/home/Dela/Movies/video.mp4

             |________________|

         relative path of video.mp4


The Special Characters

Before you get confused by the characters used in commands, let me explain some of them:

  • The (.), represents the current directory in the file system.
  • The (..), represents one level above the current directory.
  • The (/), represents the "root" of the filesystem.
  • The (~), represents the home directory of the currently logged in user.

pwd (Print Working Directory)

The pwd command is used to display the full path of the current directory you are in. It helps you determine your current location within the filesystem.

If you are in the directory '/home/Dela/Projects', typing pwd will output:

/home/Dela/Projects

This shows that you are currently in the Projects directory, which is located within the Dela home directory.


ls (List Files And Folders)

The ls command is used to list the files and directories within the current directory. It provides a basic overview of the contents, showing names of files and folders.

Simply typing ls will list the files and directories in the current location. If your current directory contains files and folders like document.txt, photo.jpg, and Projects, typing ls will output:

document.txt  photo.jpg  Projects

For more detailed information about the files and directories, you can use the -l option to get a long listing format, which includes additional details such as permissions, number of links, owner, group, size, and modification date. Combining it with the -a option will show hidden files (those starting with a dot).

ls -lah combines -l and -a to display a detailed list including hidden files, with file sizes in a human-readable format (e.g., KB, MB).  If you type ls -lah, you might see output like this:

        total 20K
        drwxr-xr-x  5 user group 4.0K Aug  3 10:00 .
        drwxr-xr-x  3 user group 4.0K Aug  1 09:00 ..
        -rw-r--r--  1 user group 1.2K Aug  2 11:00 document.txt
        -rw-r--r--  1 user group 2.5M Aug  3 09:30 photo.jpg
        drwxr-xr-x  8 user group 4.0K Aug  3 10:00 Projects      

This output provides detailed information about each item in the directory, including permissions, file sizes, and modification times.


cd (Change Directory)

Change directory (cd)

The cd command is used to change the current directory in the command line interface. This allows you to navigate through different directories within the filesystem.

- To change to a specific directory, type 'cd' followed by the directory name:

cd theFolder

This command will move you into the theFolder directory, assuming it exists within your current directory.

- Moving Up a Level:

To move up one level from your current directory, use 'cd ..'. This command takes you to the parent directory of the current directory:

cd ..

If you are currently in '/home/Dela/Projects', using 'cd ..' will take you to '/home/Dela'.

- Changing to a Subdirectory:

If you are in '/home/Dela' and want to go to the Documents folder within it, you would type:

cd Documents

- You can use absolute paths with cd to navigate directly to any directory from anywhere in the filesystem:

cd /var/log

- To return to your home directory from anywhere, simply type:

cd ~

This command helps you efficiently navigate through the directory structure, making file management and system navigation more straightforward.


cp (Copy)

The cp command is used to copy files and directories from one location to another. It is a versatile command that allows you to duplicate files or directories within the filesystem.

- To copy a file from one location to another, type 'cp' followed by the source file and the destination. For example, if you are in the Documents directory and want to copy 'myfile.txt' to the 'Desktop', you can use:

cp myfile.txt ~/Desktop

Here, '~/Desktop' represents the path to the Desktop directory in your home folder.

- Copying with Relative Paths:

If you need to copy a file from a different directory using a relative path, you can use '.' to represent the current directory. For example, if you are in the Videos directory and want to copy 'video.mp4' from the 'Downloads' folder to the current directory, you can type:

cp ~/Downloads/video.mp4 .

In this case, '.' refers to the Videos directory, where the file video.mp4 will be copied.

- Copying Directories:

To copy a directory along with its contents (subdirectories and files), use the '-r' (recursive) option. For example, to copy all files and folders from the Downloads directory to the current directory, use:

cp -r ~/Downloads/* .

The '-r' option ensures that the entire directory structure and contents are copied. Note that '*' is used to copy all items within '~/Downloads' to the current directory.

- Additional Tips:

Be cautious when copying files to avoid overwriting existing files in the destination directory. Use the '-i' (interactive) option to prompt before overwriting:

cp -i myfile.txt ~/Desktop

- Use the '-v' (verbose) option to see detailed output of what is being copied:

cp -v myfile.txt ~/Desktop

This command is essential for managing and organizing files and directories within your filesystem.


mv (Move or Rename)

The mv command is used to move or rename files and directories within the filesystem. It is a versatile command that performs both tasks, depending on how it's used.

- Moving Files:

To move a file from your current directory to another location, use mv followed by the source file and the destination. For example, to move 'myfile.txt' from your working directory to the 'Desktop', you can type:

mv myfile.txt ~/Desktop

This command transfers 'myfile.txt' to the 'Desktop' directory.

- Moving Directories:

To move a directory that contains files and other directories, you need the '-r' (recursive) option. This ensures that the entire directory structure and contents are moved. For example:

mv myFolder ~/Desktop

This command moves the entire 'myFolder' directory to the 'Desktop'. Note that '-r' is not necessary for moving directories with mv, but it's used with cp for copying.

- Renaming Files or Directories:

To rename a file or directory, use mv with the current name and the new name. For example, to rename a folder from 'oldFolderName' to 'newNameOfFolder', type:

mv oldFolderName newNameOfFolder

Similarly, to rename a file, include its extension:

mv oldFilename.txt newFilename.txt

- Additional Tips:

If a file or directory with the new name already exists at the destination, it will be overwritten without any warning. Use the '-i' (interactive) option to prompt before overwriting:

mv -i oldFilename.txt newFilename.txt

The mv command is crucial for organizing files and directories by moving or renaming them as needed.


rm (Remove or Delete)

The rm command is used to remove files and directories from the filesystem. It provides a straightforward way to delete items but should be used with caution, as deleted files and directories cannot be easily recovered.

- Removing Files:

To delete a single file, use 'rm' followed by the file name:

rm removeThis.deb

This command will delete the file 'removeThis.deb' from the current directory.

- Removing Directories:

To remove a directory and its contents, you need to use the '-r' (recursive) option, which deletes the directory and everything inside it. Additionally, the '-f' (force) option can be used to ignore non-existent files and override prompts:

rm -rf folderToRemove

Here, '-r' allows the command to delete the directory folderToRemove and its contents, while '-f' forces the deletion without any confirmation prompts.

- Additional Tips:

Use with Caution: The 'rm' command does not move items to a trash or recycle bin; once deleted, the files or directories are typically unrecoverable without special tools.

Interactive Mode: To be prompted before each file or directory is deleted, use the '-i' (interactive) option:

rm -i fileToDelete.txt

This will ask for confirmation before deleting 'fileToDelete.txt'.

Prevent Accidental Deletion: To avoid accidental deletions, especially when using -rf, double-check the command and paths to ensure you are deleting the correct items. The rm command is powerful for file and directory management, making it essential for maintaining a clean and organized filesystem.


mkdir (Make Directory)

Make directory (mkdir)

The mkdir command is used to create new directories or folders within the filesystem. It helps you organize files by creating structures and hierarchies as needed.

- To create a new directory, use 'mkdir' followed by the name of the directory you want to create:

mkdir nameYourFolder

This command will create a new directory named 'nameYourFolder' in your current working directory.

- Creating Nested Directories:

To create a directory along with its parent directories (if they do not already exist), use the '-p' (parents) option:

mkdir -p parentFolder/childFolder/grandchildFolder

This command will create 'parentFolder', 'childFolder' inside it, and 'grandchildFolder' inside 'childFolder'.

- Additional Tips:

Ensure that the directory name does not contain invalid characters or spaces, or use quotes if necessary:

mkdir "My New Folder"

After creating a directory, you can verify it with the 'ls' command to list the contents of the parent directory and confirm the new directory is present.

 

touch (Create a File)

The touch command is used to create new, empty files or to update the access and modification times of existing files. It is a simple and efficient way to create files from the command line.

- Creating a New File:

To create a new, empty file, use 'touch' followed by the file name:

touch aFile.txt

This command will create a new file named 'aFile.txt' in the 'current directory'. If the file already exists, touch will update its modification and access times to the current time.

- Additional Tips:

Update File Timestamps: If you want to change the modification or access time of an existing file without altering its contents, touch is useful for that purpose. If the specified file path includes directories that do not exist, touch will not create the directories. Ensure that the directories are created beforehand or use a tool like mkdir -p to create them.

 

less and cat (Viewing Files)

Viewing text file illustration.

Both less and cat commands are used to view the contents of files, but they serve different purposes and offer varying functionalities.

- less (View File with Navigation):

The 'less' command allows you to view the contents of a file one screen at a time. It provides advanced navigation features, such as searching and scrolling, making it useful for examining large files. To view a file with less, type:

less config.txt

This command opens 'config.txt' and allows you to scroll through the content, search for specific text, and navigate easily.

- cat (Concatenate and Display File):

The 'cat' command is used to display the entire contents of a file on the terminal at once. It is simple and straightforward but does not support advanced navigation or searching. To view a file with cat, type:

cat config.txt

This command displays the contents of 'config.txt' directly on the terminal.

For large files, less is often more practical due to its interactive features. Use cat for simpler tasks, such as checking short files or piping content to other commands.

That’s all the basic terminal commands for beginners. I hope you find this information helpful. Have a great day!