In this comprehensive guide, you’ll learn essential Linux file and directory management commands. Discover how to create, copy, move, and manage files and directories efficiently using command-line tools. Explore file permissions, directory navigation, and advanced file manipulation techniques. Whether you’re a Linux beginner or an experienced , this guide will help you master file and directory management in the Linux environment.
Linux offers a variety of commands for managing files and directories. Here is a list of some commonly used file and directory management commands:
Create an empty file or update the access and modification timestamps of an existing file.
touch filename
Display the contents of a file.
cat filename
Open a text editor to create or edit a file.
nano filename
vim filename
Copy files or directories.
cp source destination
Move or rename files and directories.
mv source destination
Remove (delete) files or directories. Be cautious with this command, as it’s irreversible.
rm filename
Display the beginning or end of a file, respectively.
head filename tail filename
view the contents of a file page by page
less filename more filename
Create a new directory.
mkdir directoryname
Remove an empty directory.
rmdir directoryname
Remove directories and their contents recursively. Use with caution
rm -r directoryname rm -rf directoryname
Change the current working directory.
cd directoryname
Display the current working directory.
pwd
List the contents of a directory.
ls
List directory contents in a long format, showing file permissions, owner, size, etc.
ls -l
List all files and directories, including hidden ones (starting with a dot).
ls -a
Display directory structure in a tree-like format.
tree
Change file permissions.
chmod permissions filename
Change file ownership.
chown newowner:newgroup filename
Change group ownership of a file.
chgrp newgroup filename
Determine the file type of a given file.
file filename
Display detailed information about a file, including access and modification times, size, and more.
stat filename
Display disk usage of files and directories.
du [options] [directory or file]
Display information about disk space usage on mounted file systems.
df [options]
Display the current working directory.
pwd
Change the current working directory.
cd directoryname
List the contents of a directory.
ls [options] [directory]
Search for files and directories in a specified location.
find directory -name filename
Quickly search for files and directories using a pre-built database (requires periodic updates with the updatedb command).
locate filename
Search for text patterns within files.
grep pattern filename
Archive files and directories into a single file (often compressed with gzip or bzip2).
tar options archive_filename files_or_directories
Compress and decompress files using the gzip compression algorithm.
gzip filename gunzip filename.gz
Create and extract zip archives.
zip archive.zip files_or_directories unzip archive.zip
Set the default permissions for newly created files and directories.
umask [permissions]
Execute commands with super (administrator) privileges.
sudo command
Switch to a different account (often used to become the super).
su name
Perform low-level data copying and conversion. Often used for creating disk images or wiping drives.
dd if=input_file of=output_file bs=block_size
Create hard or symbolic (soft) links to files or directories.
ln source_file link_name
Determine the file type of a given file.
file filename
Stream editor for text manipulation, especially useful for text substitution.
sed 's/old_text/new_text/g' filename
A versatile text processing tool for extracting and manipulating text data.
awk '{print $1}' filename
Remove sections from each line of files.
cut -ddelimiter -ffields filename
Securely copy files between hosts over SSH.
scp source_file name@hostname:destination_path
Synchronize files and directories between two locations, locally or over a network.
rsync [options] source destination
setfacl and getfacl: Set and get file access control lists (ACLs) to manage permissions more granularly.
setfacl -m u:name:permissions filename
Change file attributes, such as making a file immutable or undeletable.
chattr +i filename
Partition disk drives for creating and managing disk partitions.
fdisk /dev/sdX parted /dev/sdX
Create a filesystem on a partition.
mkfs -t filesystem_type /dev/sdX
Watch for file and directory changes and take actions based on events.
inotifywait -m /path/to/directory
List open files and the processes that have them open.
lsof /path/to/file_or_directory
Compress and decompress files using the bzip2 compression algorithm.
bzip2 filename bunzip2 filename.bz2
Edit the sudoers file using the visudo command to configure sudo access permissions for s and groups.
sudo visudo
Manage extended attributes to provide additional metadata to files and directories.
chattr +attribute filename
Use the find command with the -exec option to perform actions on files found during a search.
find /path/to/search -name "*.txt" -exec rm {} \;
Initiate and manage version control for files and directories using Git.
git init git add filename git commit -m "Commit message"
Version control system for tracking changes in files and directories using Subversion.
svn checkout repository_url svn add filename svn commit -m "Commit message"
Use advanced options with rsync to fine-tune synchronization and backup tasks.
rsync -av --delete source/ destination/
Encrypt and decrypt files using GPG for secure data storage and transmission.
gpg --encrypt filename gpg --decrypt filename.gpg
View and modify Access Control Lists (ACLs) to grant permissions to specific s or groups on files and directories.
getfacl filename setfacl -m u:name:permissions filename
Transfer files between hosts using FTP (File Transfer Protocol) or its secure counterpart, SFTP (SSH File Transfer Protocol).
ftp hostname sftp name@hostname
Here’s a multiple-choice quiz with answers based on the lesson about Linux file and directory management commands:
A) cat
B) touch
C) mkdir
D) mv
Answer: B) touch
A) ls
B) dir
C) list
D) show
Answer: A) ls
A) copy
B) move
C) cp
D) mv
Answer: C) cp
A) cd
B) pwd
C) dir
D) change
Answer: A) cd
A) delete
B) remove
C) rm
D) erase
Answer: C) rm
A) To change the current working directory.
B) To change file ownership.
C) To change file permissions.
D) To list files and directories.
Answer: C) To change file permissions.
A) ln
B) link
C) sl
D) symlink
Answer: A) ln
A) info
B) details
C) stat
D) inspect
Answer: C) stat
A) find
B) search
C) locate
D) grep
Answer: A) find
A) scp
B) copy
C) rsync
D) sftp
Answer: A) scp
A) make
B) newdir
C) mkdir
D) create
Answer: C) mkdir
A) dir
B) current
C) pwd
D) showdir
Answer: C) pwd
A) list -a
B) showall
C) ls -h
D) ls -a
Answer: D) ls -a
A) chown
B) own
C) changeowner
D) modifyowner
Answer: A) chown
A) compress
B) zip
C) tar
D) gzip
Answer: C) tar
A) replace
B) awk
C) edit
D) replace
Answer: B) awk
A) To synchronize time between servers.
B) To create symbolic links.
C) To synchronize files and directories between two locations.
D) To list open files and processes.
Answer: C) To synchronize files and directories between two locations.
A) ftp
B) sftp
C) transfer
D) copy
Answer: B) sftp
A) mkfs
B) createfs
C) format
D) fscreate
Answer: A) mkfs
A) setperm
B) umask
C) chmod
D) permissions
Answer: B) umask
A) zip
B) compress
C) tar
D) gzip
Answer: D) gzip
A) attr
B) modifyattr
C) chattr
D) fileattr
Answer: C) chattr
A) To list files and directories.
B) To change file permissions.
C) To create hard or symbolic links to files or directories.
D) To move files and directories.
Answer: C) To create hard or symbolic links to files or directories.
A) chgroup
B) modifygroup
C) groupchange
D) chgrp
Answer: D) chgrp
A) scp
B) securecopy
C) sshd
D) copyssh
Answer: A) scp
A) compress
B) unzip
C) zip
D) zipextract
Answer: C) zip
A) switch
B) su
C) change
D) change
Answer: B) su
A) treetop
B) treestructure
C) treeview
D) tree
Answer: D) tree
A) setacl
B) aclmanage
C) acl
D) getfacl and setfacl
Answer: D) getfacl and setfacl
A) version
B) control
C) git
D) gitinit
Answer: C) git