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
Listing Files with Details:
Displays detailed information about files and directories.
Changing Directories:
Navigates to the
/var/log
directory.Copying a File:
Copies
file1.txt
to/tmp/
and renames it tofile2.txt
.Removing a Directory Recursively:
Deletes
test_directory
and its contents.Viewing File Contents with Line Numbers:
Displays the contents of
file.txt
with line numbers.
Interview Questions and Answers
Q: What is the purpose of the
ls
command? How would you list all hidden files? A: Thels
command lists files and directories in the current directory. To list hidden files, usels -a
, which includes files starting with a dot (.
).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 simplycd
without any arguments.Q: What is the difference between
rm -r
andrmdir
? A:rm -r
removes a directory and all its contents recursively, whilermdir
only removes empty directories.Q: Explain the use of the
mv
command. Can it be used to rename files? A: Themv
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
.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.Q: What is the difference between
cat
,more
, andless
? A:cat
displays the entire file contents at once,more
shows contents one screen at a time but only allows forward navigation, andless
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