06 - 09 - 2024 - Linux Commands
06 - 09 - 2024 - Linux Commands
head Shows the first few lines of a file. -n N : Displays the first N lines of the file. head -n 5 file.txt
tail Shows the last few lines of a file. -n N : Displays the last N lines of the file. tail -n 5 file.txt
id
Displays the current user ID (UID) and -u: Shows only the UID.
id -u
id group ID (GID), as well as the -g: Shows only the GID.
id -g
supplementary group IDs. -n: Shows the names instead of the numerical IDs.
id -n
-a: Adds a supplementary group to the user account. usermod -a -G developers alice
-G: Changes the primary group of the user account. usermod -G staff alice
usermod Modifies an existing user account.
-l: Renames the user account. usermod -l alice_newname alice
-d: Changes the home directory of the user account. usermod -d /home/alice_newdir alice
-remove-home: Removes the user's home directory and mail deluser alice
deluser Deletes a user account.
spool. deluser alice -remove-home
Runs a process at a different priority value: Sets the new priority value. nice -n 10 command
nice
level. -n: Renices an already running process. renice -n 5 -p PID
du
Displays disk usage statistics for files -h: Shows output in human-readable format.
du du -h
and directories. -s: Displays only a summary for the specified directory.
du -sh /home/user
Counts the number of lines, words, and -l: Displays only the line count.
wc myfile.txt
wc characters in files or input from other -w: Displays only the word count.
wc -l file.txt
commands. -c: Displays only the character count.
Extracts specific columns from files or -f: Specifies the field(s) to extract.
cut cut -f1 -d":" /etc/passwd
input from other commands. -d: Specifies the delimiter character.
{print $1}: Displays only the first column.{sum += $2} END {print awk '{print $1}' file.txt
Searches and manipulates text files
awk sum}: Calculates the sum of the second column./pattern/ {print awk '{sum += $2} END {print sum}' data.txt
according to patterns and actions.
$0}: Searches for a pattern and displays the entire line. awk '/error/ {print $0}' logfile.txt
2. Displays the contents of `file.txt` and sorts the lines in reverse alphabetical order.
cat file.txt | sort -r
3. Searches for all files modified within the last 7 days in the `/home/user` directory and its subdirectories, and filters the results to only show files containing "log".
find /home/user -type f -mtime -7 | grep "log"
4. Displays a list of running processes, filters out those that do not contain "chrome", and extracts the second column (process ID).
ps aux | grep "chrome" | awk '{print $2}'
5. Displays the most recent entries in the system log file and filters out any lines that do not contain "error".
tail -f /var/log/syslog | grep "error"
6. Displays the last 10 commands executed in the terminal history and removes the timestamp and leading spaces.
history | tail -n 10 | cut -d " " -f 4-
10. Listens on port 1234 for incoming network connections, saves any received data to a file `received.txt`, and filters out any lines that do not contain "pattern".
nc -l -p 1234 | tee received.txt | grep "pattern"
11. Lists files in the current directory (human-readable format) sorted by size (largest first) and displays only the 10 largest files.
ls -lh | sort -k 5nr | head -n 10
12. Downloads the HTML content of a website, filters out the `<title>` tag, and removes any remaining HTML tags using the `sed` command.
curl https://example.com | grep "<title>" | sed 's/<[^>]*>//g'
13. Counts the frequency of commands executed in the terminal history, sorts them by frequency, and displays the top 10 most commonly used commands.
history | awk '{print $2}' | sort | uniq -c | sort -nr | head -n 10
14. Displays the most recent entries in the system log file that contain the word "error" and extracts the message from the sixth column.
tail -f /var/log/syslog | awk '/error/ {print substr($0, index($0,$6))}'
15. Concatenates the contents of two files, searches for the word "keyword" (case-insensitive), and counts the number of lines that match the search pattern.
cat file1.txt file2.txt | grep -i "keyword" | wc -l
16. Lists running processes sorted by CPU usage (lowest first) and displays only the 10 with the lowest usage.
ps aux | sort -nk 3 | tail -n 10
18. Finds all `.txt` files in the current directory and its subdirectories, and moves them to a directory named `backup`.
find . -type f -name "*.txt" -exec mv {} backup \;
Author: [email protected] – FPT University Da Nang.
19. Calculates the sum of the second column (separated by commas) in a CSV file.
cat file.txt | awk -F "," '{sum += $2} END {print sum}'
20. Gets the hostname of the current machine, pings it once, and displays the results in the terminal.
uname -a | cut -d " " -f 2 | xargs ping -c 1
1. Running a script every day at 6:00am: 12. Running a script every 5 minutes from 8:00am to 5:00pm, Monday
through Friday:
0 6 * * * /path/to/script.sh
2. Running a backup script every Sunday at 11:00pm: */5 8-17 * * 1-5 /path/to/script.sh
13. Running a script every 10 minutes on Sundays:
0 23 * * sun /path/to/backup.sh
3. Running a cleanup script every Monday at 3:30am: */10 * * * sun /path/to/script.sh
14. Running a script at 6:00am and 6:00pm every day:
30 3 * * mon /path/to/cleanup.sh
4. Running a database backup every Friday at 10:00pm: 0 6,18 * * * /path/to/script.sh
15. Running a script every Friday and Saturday night at 11:30pm:
0 22 * * fri /path/to/db-backup.sh
5. Running a script every hour on the half-hour: 30 23 * * fri,sat /path/to/script.sh
16. Running a script every 4 hours from 9:00am to 5:00pm, Monday
30 * * * * /path/to/script.sh
through Friday:
6. Running a script every 15 minutes:
0 */4 9-17 * * 1-5 /path/to/script.sh
*/15 * * * * /path/to/script.sh
17. Running a script every other day at 3:00am:
7. Running a script at 2:30am every day:
0 3 */2 * * /path/to/script.sh
30 2 * * * /path/to/script.sh
18. Running a script every 10 minutes during business hours (8:00am to
8. Running a script every weekday at 9:00am: 5:00pm), Monday through Friday:
0 9 * * 1-5 /path/to/script.sh */10 8-17 * * 1-5 /path/to/script.sh
9. Running a script every first day of the month at midnight: 19. Running a script every 30 minutes on the first 10 days of each month:
0 0 1 * * /path/to/script.sh */30 * 1-10 * * /path/to/script.sh
10. Running a script every Sunday at midnight: 20. Running a script every hour from 7:00am to 7:00pm, Monday through
0 0 * * sun /path/to/script.sh Friday:
11. Running a script every 30 minutes: 0 7-19 * * 1-5 /path/to/script.sh
*/30 * * * * /path/to/script.sh
1. Create a folder: This script uses the mkdir command to create a new folder/directory.
#!/bin/bash
echo "Enter folder name:"
read folder_name
mkdir $folder_name
echo "Folder created successfully"
5. List directory contents: This script uses the ls command to list the contents of a directory.
#!/bin/bash
echo "Enter directory name:"
read dir_name
ls $dir_name
6. Change permissions for a file: This script uses the chmod command to change the permissions for a file.
#!/bin/bash
echo "Enter file name:"
read file_name
echo "Enter permission (e.g. 755):"
read permission
chmod $permission $file_name
echo "File permissions changed successfully"
7. Add a user: This script uses the useradd command to add a new user.
#!/bin/bash
echo "Enter username:"
read username
useradd $username
echo "User added successfully"
8. Change password for a user: This script uses the passwd command to change the password for a user.
#!/bin/bash
echo "Enter username:"
read username
passwd $username
echo "Password changed successfully"
9. Create a group: This script uses the groupadd command to create a new group.
#!/bin/bash
echo "Enter group name:"
read group_name
groupadd $group_name
echo "Group created successfully"
10. Check memory usage: This script uses the free command to check the memory usage.
#!/bin/bash
free -m