In this tutorial, you will learn how to use basic shell commands for automation. Shell commands provide a powerful and flexible way to interact with the computer, and understanding them is key for tasks such as batch file processing, system administration, and automation.
You will learn how to:
Prerequisites: Basic knowledge of computer systems. No prior experience with shell scripting is required.
ls
: Lists all files and directories in the current directory.cd
: Changes the working directory.mkdir
: Creates a new directory.rm
: Removes files or directories.cp
: Copies files or directories.mv
: Moves or renames files or directories.cat
: Concatenates and displays file content.echo
: Outputs the strings it is being passed as arguments.grep
: Searches for a pattern in files.sed
: Stream editor for filtering and transforming text.for
: Executes commands for each item in a list.while
: Executes commands as long as a condition is true.crontab
: Schedules tasks to run at fixed times.# List all files and directories
ls
# Change directory to Documents
cd Documents
# Make a directory called new_directory
mkdir new_directory
# Remove a file called unwanted_file.txt
rm unwanted_file.txt
# Copy file.txt to the new_directory
cp file.txt new_directory/
# Move file.txt to the new_directory
mv file.txt new_directory/
# Display the content of file.txt
cat file.txt
# Print 'Hello, World!' to the console
echo 'Hello, World!'
# Search for 'pattern' in file.txt
grep 'pattern' file.txt
# Replace all occurrences of 'old' with 'new' in file.txt
sed 's/old/new/g' file.txt
# Print numbers 1 to 5
for i in {1..5}; do echo $i; done
# Keep printing 'Hello, World!' until stopped
while true; do echo 'Hello, World!'; sleep 1; done
# Schedule a task to run at 12:00 every day
echo "0 12 * * * /path/to/command" | crontab -
In this tutorial, you have learned how to use basic shell commands for file and directory management, text processing, and automation. With these skills, you are ready to start automating tasks on your computer.
Next steps:
Additional resources:
Solutions:
for i in {1..10}; do echo $i; done
mkdir temp_directory; echo 'Hello, World!' > temp_directory/temp_file.txt; rm -r temp_directory
sed 's/apple/orange/g' input_file.txt > output_file.txt
Keep practicing to become more proficient. Happy scripting!