Chapter 8: Working with Files and Directories
Learn how to manage files and directories in Bash, including creating, deleting, copying, moving files, changing permissions, and navigating the filesystem.
In this chapter, we’ll explore essential commands for working with files and directories in Bash, allowing you to efficiently navigate and manipulate the filesystem within your scripts.
Navigating the Filesystem
To move between directories in Bash, use the cd
command:
# Changing to a specific directory
cd /path/to/directory
# Moving up one level
cd ..
# Returning to the previous directory
cd -
Creating Files and Directories
Bash provides commands to create new files and directories:
touch
: Creates a new, empty file.mkdir
: Creates a new directory.
# Example: Creating a file and directory
touch newfile.txt
mkdir new_directory
Copying and Moving Files
The cp
and mv
commands allow you to copy and move files and directories:
# Copying a file
cp file.txt /path/to/destination
# Moving (or renaming) a file
mv file.txt newname.txt
In the first example, file.txt
is copied to a new location. In the second, file.txt
is renamed to newname.txt
.
Deleting Files and Directories
To remove files or directories, use the rm
and rmdir
commands:
# Removing a file
rm file.txt
# Removing an empty directory
rmdir empty_directory
# Removing a directory and its contents
rm -r directory_name
The -r
flag is used with rm
to delete a directory and all its contents.
Viewing and Changing File Permissions
Each file and directory has specific permissions for the owner, group, and others. Use ls -l
to view permissions and chmod
to change them:
# Viewing permissions
ls -l file.txt
# Changing permissions
chmod 644 file.txt # Read/write for owner, read-only for group and others
In the example, chmod 644 file.txt
sets the file permissions so that the owner can read and write, while the group and others can only read.
Finding Files and Directories
The find
command is a powerful tool for searching files and directories based on name, type, and other criteria:
# Finding files by name
find /path/to/search -name "filename.txt"
# Finding directories
find /path/to/search -type d -name "directory_name"
In these examples, find
searches for files or directories matching the specified name within a given path.
Reading File Contents
Use commands like cat
, less
, and tail
to view file contents:
cat
: Displays the entire file contents.less
: Allows for interactive reading, scrolling through the file.tail
: Shows the last few lines of a file (useful for monitoring logs).
# Viewing the content of a file
cat file.txt
# Viewing the last 10 lines of a file
tail file.txt
Summary and Next Steps
In this chapter, we explored how to work with files and directories in Bash, including creating, copying, moving, and deleting files and directories, as well as changing permissions and searching for files. In the next chapter, we’ll look at process management in Bash, enabling you to control and monitor running processes within your scripts.