Part 3: Working with Files and Directories
Welcome back to our “Mastering Bash Scripting for Embedded Linux Development“ series! In this third part, we’ll explore essential techniques for working with files and directories in Bash scripting. From file manipulation to directory traversal and pattern matching, mastering these concepts will empower you to efficiently manage and manipulate data within your embedded Linux systems.
File Manipulation
Bash scripting provides powerful tools for manipulating files, including creating, reading, writing, and deleting them.
Creating a File
Use the touch a
command followed by the filename to create an empty file. For example,
# Create a new file
touch new_file.txt
Besides touch
, you can also use redirection with echo
to create files with content. For instance:
Reading from a File:
The cat
command is handy for displaying the contents of a file directly in the terminal. For example,
# Read the contents of a file
cat greeting.txt
Apart from cat
, you can use head
or tail
to display the beginning or end of a file, respectively. For example:
head greeting.txt
Note: When using the head
command, by default, it prints the first 10 lines of a file. Similarly, the tail
command prints the last 10 lines of a file by default. However, if the file contains fewer than 10 lines, head
or tail
will print all the lines available in the file. For example, executing tail greeting.txt
will print the last 10 lines of the greeting.txt
file if it contains 10 or more lines. If the file contains fewer than 10 lines, tail
will print all of them.
Writing to a File
You can use redirection (>
or >>
) to write to a file. For example,
# Write to a file
echo "Hello, world!" > greeting.txt
Append content to a file using >>
. For example, to add more text to greeting.txt
:
# Append to a file
echo "Welcome to Embed Threads" >> greeting.txt
Deleting a File
The rm
command followed by the filename removes a file. Be cautious when using rm
as it deletes files permanently without confirmation. For example,
# Delete a file
rm greeting.txt
Note: Use wildcards with rm
to delete multiple files. For instance, to remove all .txt
files in the current directory: rm *.txt
Directory Traversal
Navigating directories and accessing files within them is a common task in Bash scripting. You can use commands like cd
, ls
, and pwd
to traverse directories and perform operations on files.
Changing Directories
Use the cd
command followed by the directory name to change your current working directory. For example,
# Change directory
cd /path/to/directory
Listing Contents
The ls
command lists the contents of a directory. Adding options such as -l
provides detailed information about files, including permissions and ownership.
# List files in the current directory
ls
# Print the current directory
pwd
cd ~
: Changes the current directory back to the user’s home directory (/home/alok
).cd "Embed Threads"
: Changes the current directory to a directory namedEmbed Threads
. However, note that the directory name contains a space, so it should be enclosed in quotes to be interpreted as a single argument.cd ..
: Moves up one directory from/home/alok/Embed Threads
to/home/alok
.cd documents
: Attempts to change the directory to a directory nameddocuments
. However, it results in an error messagebash: cd: documents: No such file or directory
because there is no directory nameddocuments
in the current directory (/home/alok
).
Moving Files
You can move files between directories using mv
. For instance, to move file.txt
from the current directory to Documents
:
mv file.txt Documents/
Copying Files
Similarly, copy files with cp
. For example, to copy file.txt
to a backup folder:
cp file.txt backup/
File Permissions and Ownership
Understanding file permissions and ownership is crucial for securing your files and controlling access. In Linux systems, each file has permissions for the owner, group, and others, denoted by read (r
), write (w
), and execute (x
).
- Viewing Permissions: Utilize the
ls -l
command to view permissions. Permissions are displayed as a sequence of characters, such as-rwxr-xr--
, where the first three characters represent permissions for the owner, the next three for the group, and the last three for others. - Changing Permissions: The
chmod
command allows you to change file permissions. Syntax:chmod [permissions] filename
. For instance,chmod +x script.sh
adds execute permissions toscript.sh
. - Ownership: The
chown
command changes the ownership of files. Syntax:chown [user]:[group] filename
. For example,chown user:group example.txt
changes the ownership ofexample.txt
.
The string -rwxr-xr-x 1 root root 0 Mar 24 13:18 script.sh
represents the file permissions and metadata associated with the file named script.sh
. Let’s break down each part of this string:
- Permissions: The first part of the string
-rwxr-xr-x
indicates the permissions of the file. Each character represents the permissions for the file’s owner, the group the file belongs to, and others (users who are not the owner and not in the group). The characters have the following meanings:-
indicates that the corresponding permission is not granted.r
indicates read permission.w
indicates write permission.x
indicates execute permission.
rwx
indicates that the owner of the file (root
) has read, write, and execute permissions.r-x
indicates that the group (root
) has read and execute permissions but not write permissions.r-x
indicates that others (users who are not the owner and not in the group) have read and execute permissions but not write permissions.
- Number of Hard Links: The number
1
after the permissions indicates the number of hard links to the file. Hard links are additional names for the same file on the file system. In this case, there is only one hard link to the file. - Owner and Group: The next two entries
root root
indicate the owner and group of the file. In this case, the owner isroot
and the group is alsoroot
. - File Size: The number
0
indicates the size of the file in bytes. In this case, the file size is0
, meaning the file is empty. - Date and Time:
Mar 24 13:18
represents the date and time when the file was last modified or created. In this case, the file was modified or created on March 24th at 13:18 (1:18 PM). - Filename: Finally,
script.sh
is the name of the file.
The above explanation illustrates symbolic notation, but you can also utilize octal numbers with the chmod
command.
- Read (r) is represented by the value 4.
- Write (w) is represented by the value 2.
- Execute (x) is represented by the value 1.
- No permission (-) is represented by the value 0.
To determine the octal value of a set of permissions, you simply add up the values for each permission. For example:
- If a file has read, write, and execute permissions for the owner, the octal value for the owner’s permissions would be 4 (read) + 2 (write) + 1 (execute) = 7.
- If a file has only read and execute permissions for the group, the octal value for the group’s permissions would be 4 (read) + 0 (no write) + 1 (execute) = 5.
- If a file has only read permission for others, the octal value for others’ permissions would be 4 (read) + 0 (no write) + 0 (no execute) = 4.
So, in octal notation, the file permissions -rwxr-xr-x
would be represented as 755
:
- Owner: Read (4) + Write (2) + Execute (1) = 7
- Group: Read (4) + Execute (1) = 5
- Others: Read (4) + Execute (1) = 5
To modify file permissions using octal notation, you would use the chmod
command followed by the octal representation of the desired permissions. For example:
# Change file permissions
chmod 755 script.sh
Bash Scripting
Example 1: Create 10 Files with Random String
#!/bin/bash
# This script creates 10 files with random strings as their content
# Define files to create
files=("file1.txt" "file2.txt" "file3.txt" "file4.txt" "file5.txt"
"file6.txt" "file7.txt" "file8.txt" "file9.txt" "file10.txt")
# Create files with random content
for file in "${files[@]}"; do
# Generate a random string
random_string=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
echo "$random_string" > "$file"
done
echo "Files created successfully!"
Example 2: Automated File Backup Script
#!/bin/bash
# This script creates a backup of specified files
# Define files to backup
files="file1.txt file2.txt"
# Create a backup directory if it doesn't exist
backup_dir="backup"
if [ ! -d "$backup_dir" ]; then
mkdir "$backup_dir"
fi
# Copy files to the backup directory
for file in $files; do
cp "$file" "$backup_dir/"
done
echo "Backup completed successfully!"
Example 3: File Permission Checker
#!/bin/bash
# This script checks the permissions of a file
# Define file to check
file="file1.txt"
# Check file permissions
permissions=$(ls -l "$file" | cut -d ' ' -f 1)
echo "Permissions of $file: $permissions"
File Globbing and Pattern Matching
Bash provides powerful mechanisms for matching filenames using patterns:
Wildcard Characters
Use wildcard characters such as *
(matches any string) and ?
(matches any single character) for pattern matching. For example, ls *.txt
lists all files with a .txt
extension.
# List all files with a .txt extension
ls *.txt
# Delete all files starting with "temp"
rm temp*
Brace Expansion
Brace expansion allows you to generate arbitrary strings. For example, echo file{1..3}.txt
will display file1.txt
, file2.txt
, and file3.txt
.
cat file{1..3}.txt
Character Classes
Match files with specific characters. For instance, to list files with numbers in the name:
ls *[0-9]*
Bash Scripting
Example 4: Pattern Matching Script
#!/bin/bash
# This script lists all text files in the current directory
# List text files
echo "Text files:"
ls *.txt
Example 5: File Cleanup Script
#!/bin/bash
# This script deletes all text files in the current directory
# Delete all text files
find . -name "*.txt" -type f -delete
echo "Text files deleted successfully!"
In this part of our series, we’ve explored essential techniques for working with files and directories in Bash scripting. From file manipulation to directory traversal and pattern matching, these concepts are foundational for managing data within your embedded Linux systems.
FAQ
Q: Can I create a file and a directory with the same name?
A: No, a file and a directory cannot share the same name in the same location. However, they can have the same name if they are in different locations.
Q: How can I view the contents of a file without using cat
, less
, or more
?
A: You can use the head
command to display the first part of a file, or the tail
command to display the last part of a file.
Q: What is the difference between >
and >>
?
A: The >
operator overwrites the file with the output of the command, while the >>
operator appends the output to the end of the file.
Q: How can I delete a directory that is not empty?
A: You can use the rm -r
command to remove a directory and its contents. Be careful with this command, as it will delete the directory and all of its contents without asking for confirmation.
For more detailed information, you can always refer to the official GNU Bash documentation.
That’s all for today’s post.
Stay curious, keep exploring, and happy scripting!
This concludes Part 3 of our series. Stay tuned for future installments!
Leave a comment