Basic Commands
What is the command to list all files in a directory, including hidden files?
ls -a
The -a option lists all files, including hidden ones that start with a dot (.).
How do you display the contents of a file?
cat filename
Other commands like more, less, head, and tail can also be used to display file contents.
How do you copy a file from one location to another?
cp source_file destination_file
How do you move or rename a file?
mv old_name new_name
mv file_name /path/to/new_location/
How do you remove a file and a directory?
rm file_name
rm -r directory_name
File Permissions
How do you change the permissions of a file?
chmod 755 filename
This sets the file permissions to rwxr-xr-x.
How do you change the owner of a file?
chown new_owner filename
How do you change the group ownership of a file?
chgrp new_group filename
What does chmod 644 filename do?
It sets the file permissions to rw-r--r--, which means the owner can read and write, while the group and others can only read.
Process Management
How do you list currently running processes?
ps
Use ps aux for a more detailed view and top or htop for a dynamic view.
How do you kill a process by its process ID (PID)?
kill PID
Use kill -9 PID to force kill a process.
How do you bring a background process to the foreground?
fg
How do you send a process to the background?
Bg
Use & at the end of a command to start it in the background:
command &
File Management
How do you find files in a directory with a specific pattern?
find /path -name "*.txt"
This finds all .txt files in the specified path.
How do you search for a specific text in files?
grep "text_to_search" filename
Use grep -r "text_to_search" /path to search recursively.
How do you count the number of lines, words, and characters in a file?
wc filename
You can also use wc -l, wc -w, and wc -c for lines, words, and characters, respectively.
How do you display the first and last 10 lines of a file?
head filename
tail filename
Use head -n 20 filename and tail -n 20 filename to display the first and last 20 lines.
Scripting
How do you create a simple shell script?
#!/bin/bash
echo "Hello, World!"
Save the file and make it executable:
chmod +x script_name.sh
./script_name.sh
How do you set a variable in a shell script?
variable_name="value"
echo $variable_name
How do you read input from a user in a shell script?
read -p "Enter your name: " name
echo "Hello, $name"
How do you write an if statement in a shell script?
if [ $variable -eq 1 ]; then
echo "Variable is equal to 1"
Else
echo "Variable is not equal to 1"
fi
File Compression
How do you compress files using gzip and tar?
gzip filename
tar -czvf archive_name.tar.gz directory_name
How do you extract compressed files?
gunzip filename.gz
tar -xzvf archive_name.tar.gz
Networking
How do you display network interfaces and IP addresses?
Ifconfig
Use ip addr show for more modern systems.
How do you test network connectivity?
ping hostname_or_ip
How do you transfer files between systems using scp?
scp source_file user@remote_host:/path/to/destination
System Administration
How do you check disk usage in Unix?
df -h
How do you check memory usage?
free -m
How do you change the hostname of a Unix system?
sudo hostname new_hostname
How do you schedule a job using cron?
Edit the crontab file using crontab -e and add a line in the format:
* * * * * /path/to/command
The five asterisks represent the minute, hour, day of the month, month, and day of the week, respectively.
Comments
Post a Comment