Basic File and Directory commands

Chapter 1: Basic File and Directory Commands

In this chapter, we'll cover essential Linux commands used for navigating and managing files and directories. Understanding these commands is crucial for efficiently working with the Linux filesystem.

Commands Table

Command

Common Arguments

Examples

Description

ls

-l, -a, -h, -R

ls -l (long listing), ls -a (show hidden files), ls -h (human-readable sizes), ls -R (recursive)

Lists the contents of a directory, including file details, hidden files, sizes, and recursive listings.

cd

<directory>

cd /home/user, cd .. (move up one level), cd ~ (go to home)

Changes the current working directory.

pwd

None

pwd

Prints the current working directory path.

mkdir

-p, <directory_name>

mkdir test, mkdir -p parent/child

Creates a new directory. -p creates parent directories as needed.

rmdir

<directory_name>

rmdir test

Removes an empty directory.

cp

-r, -v, -i

cp file1 file2, cp -r dir1 dir2 (copy directory), cp -i file1 file2 (prompt before overwrite)

Copies files and directories. -r for directories, -i prompts before overwrite.

mv

-i, -v

mv oldname newname, mv -i file1 file2

Moves or renames files and directories. -i prompts before overwrite.

rm

-r, -f, -i

rm file, rm -r dir (remove directory), rm -i file (prompt before deletion)

Deletes files or directories. -r for directories, -f forces deletion, -i prompts before delete.

cat

-n, <file>

cat file.txt, cat -n file.txt (number lines)

Concatenates and displays file contents.

more

None

more file.txt

Displays file contents one screen at a time.

less

None

less file.txt

Displays file contents, allowing backward and forward navigation.

Command Examples

  1. Listing Files with Details:

    ls -l

    Displays detailed information about files and directories.

  2. Changing Directories:

    cd /var/log

    Navigates to the /var/log directory.

  3. Copying a File:

    cp file1.txt /tmp/file2.txt

    Copies file1.txt to /tmp/ and renames it to file2.txt.

  4. Removing a Directory Recursively:

    rm -r test_directory

    Deletes test_directory and its contents.

  5. Viewing File Contents with Line Numbers:

    cat -n file.txt

    Displays the contents of file.txt with line numbers.

Interview Questions and Answers

  1. Q: What is the purpose of the ls command? How would you list all hidden files? A: The ls command lists files and directories in the current directory. To list hidden files, use ls -a, which includes files starting with a dot (.).

  2. Q: How would you navigate back to your home directory from any location in the terminal? A: You can navigate back to your home directory using cd ~ or simply cd without any arguments.

  3. Q: What is the difference between rm -r and rmdir? A: rm -r removes a directory and all its contents recursively, while rmdir only removes empty directories.

  4. Q: Explain the use of the mv command. Can it be used to rename files? A: The mv command is used to move or rename files and directories. To rename a file, specify the current name and the new name: mv oldname.txt newname.txt.

  5. Q: How can you safely copy files to avoid accidentally overwriting existing ones? A: Use the cp -i command, which prompts before overwriting files, allowing you to confirm each action.

  6. Q: What is the difference between cat, more, and less? A: cat displays the entire file contents at once, more shows contents one screen at a time but only allows forward navigation, and less allows both forward and backward navigation through the file.

This chapter covers the essential commands that form the foundation of working with Linux files and directories. Mastery of these commands is critical for navigating the system and managing files effectively in any Linux environment.

Last updated