Shell Commands
As said in the lecture shell commands....
To successfully make use of a terminal and to become competent, knowing the commonly used posix and gnu-core-utils packages are paramount. It is a tall order to learn every package, out there, for instance the nixos package manager has over 80,000 packages!
So below is a sample of useful packages to become familar with.
1. Navigating the File System
1.1 pwd (Print Working Directory)
Description: Displays the current directory.
pwd
Example:
- Open your terminal.
- Type
pwdand press Enter. - You will see the full path of the directory you’re currently in, like
/home/user.
1.2 cd (Change Directory)
Description: Changes the working directory.
cd [directory]
Examples:
- To move to the
/homedirectory:cd /home - To go back to the previous directory:
cd -
1.3 ls (List Directory Contents)
Description: Lists files and directories.
ls [options] [directory]
Examples:
- List all files in the current directory:
ls - List hidden files:
ls -a - Show detailed information (file size, permissions, etc.):
ls -l
2. File Operations
2.1 cp (Copy Files)
Description: Copies files or directories.
cp [source] [destination]
Examples:
- Copy a file:
cp file.txt /backup/file.txt - Copy a directory:
cp -r /source/dir /backup/dir
2.2 mv (Move/Rename Files)
Description: Moves or renames files.
mv [source] [destination]
Examples:
- Move a file:
mv file.txt /backup/file.txt - Rename a file:
mv oldname.txt newname.txt
2.3 rm (Remove Files)
Description: Deletes files or directories.
rm [options] [file/directory]
Examples:
- Remove a file:
rm file.txt - Remove a directory:
rm -r /path/to/directory
3. File Permissions
3.1 chmod (Change File Permissions)
Description: Changes permissions of a file or directory.
chmod [options] [permissions] [file/directory]
Examples:
- Make a file executable:
chmod +x script.sh - Set specific permissions (e.g., read-write for owner, read for others):
chmod 644 file.txt
3.2 chown (Change File Ownership)
Description: Changes the owner of a file or directory.
chown [owner][:group] [file/directory]
Examples:
- Change owner:
chown newuser file.txt - Change owner and group:
chown newuser:newgroup file.txt
4. Searching and Finding Files
4.1 find (Search for Files)
Description: Searches for files in a directory hierarchy.
find [path] [options]
Examples:
- Find all
.txtfiles in the current directory:find . -name "*.txt" - Find files modified in the last 7 days:
find /path -mtime -7
4.2 grep (Search Text in Files)
Description: Searches for a pattern in files.
grep [options] [pattern] [file]
Examples:
- Search for "hello" in a file:
grep "hello" file.txt - Search recursively in a directory:
grep -r "hello" /path/to/directory
5. Disk Usage
5.1 df (Disk Free)
Description: Displays disk space usage.
df [options]
Example:
- Check disk usage in human-readable format:
df -h
5.2 du (Disk Usage)
Description: Shows the size of files and directories.
du [options] [file/directory]
Example:
- Check the size of a directory:
du -sh /path/to/directory
6. Process Management
6.1 ps (Process Status)
Description: Shows information about active processes.
ps [options]
Example:
- Show all running processes:
ps aux
6.2 kill (Terminate Processes)
Description: Sends a signal to a process (usually to terminate it).
kill [options] [process_id]
Example:
- Kill a process with PID 1234:
kill 1234
7. Networking
7.1 ping (Check Network Connectivity)
Description: Sends ICMP echo requests to test network connectivity.
ping [host]
Example:
- Ping Google to check internet connectivity:
ping google.com
7.2 ifconfig / ip (Network Interface Configuration)
Description: Displays or configures network interfaces.
ifconfig [interface]
or
ip a
Example:
- Check the network interface configuration:
ifconfig
or
ip a
8. Archiving and Compression
8.1 tar (Create and Extract Archives)
Description: Creates or extracts archives.
tar [options] [archive] [file/directory]
Examples:
- Create a
.tar.gzarchive:tar -czvf archive.tar.gz /path/to/directory - Extract a
.tar.gzarchive:tar -xzvf archive.tar.gz
8.2 gzip / gunzip (Compress/Decompress Files)
Description: Compresses or decompresses files using gzip.
gzip [file]
gunzip [file.gz]
Examples:
- Compress a file:
gzip file.txt - Decompress a file:
gunzip file.txt.gz
9. Text Processing
9.1 cat (Concatenate and Display Files)
Description: Displays file contents.
cat [file]
Example:
- Display the contents of a file:
cat file.txt
9.2 head / tail (View Beginning or End of Files)
Description: Displays the first or last lines of a file.
head [file]
tail [file]
Example:
- Display the first 10 lines of a file:
head file.txt - Display the last 10 lines of a file:
tail file.txt
10. Dates
-
First run the
datecommand in the terminal.$ dateOutput: will be something like this...
Mon 27 Feb 13:58:41 GMT 2023 -
So how do we get different formats?
Well if in doubt you can check the
manpages or use the commands--helpflag$ date --help -
Looking at the output workout how to produce this format:
> 20230227Answer
date +"%Y%m%d" -
Now modify this output to display:
> Year: 2023, Month: 02, Day: 27Answer
date +"Year: %Y, Month: %m, Day: %d" -
The most common formatting characters:
%D– Display date as mm/dd/yy%Y– Year (e.g., 2020)%m– Month (01-12)%B– Long month name (e.g., November)%b– Short month name (e.g., Nov)%d– Day of month (e.g., 01)%j– Day of year (001-366)%u– Day of week (1-7)%A– Full weekday name (e.g., Friday)%a– Short weekday name (e.g., Fri)%H– Hour (00-23)%I– Hour (01-12)%M– Minute (00-59)%S– Second (00-60)
-
Using the above information and what previous steps reproduce the ISO 8601 standard for datetime.
> 2023-02-27T12:08:45ZAnswer
$ date +"%Y-%m-%dT%H:%M:%S:%NZ"%Y– Year (e.g., 2020)%m– Month (01-12)%d– Day of month (e.g., 01)T– self delimeter%H– Hour (00-23):– self delimeter%M– Minute (00-59):– self delimeter%S– Second (00-60)Z– self delimeter
-
Amend the last command so that after seconds you get nano seconds.
> 2023-02-27T12:14:03:281999558ZAnswer
$ date +"%Y-%m-%dT%H:%M:%S:%NZ"%Y– Year (e.g., 2020)%m– Month (01-12)%d– Day of month (e.g., 01)T– self delimeter%H– Hour (00-23):– self delimeter%M– Minute (00-59):– self delimeter%S– Second (00-60):– self delimeter%N– nanoseconds (000000000..999999999)Z– self delimeter
11 Manipulating inputs and outputs
-
working with the previous command lets strip out the
T,:s andZso that we are left with only numnbers. -
Using
awkwe can manipulate the output by piping|the output ofdateinto awk - first run this command$ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk "{print $1}" > 2023-02-27T12:20:40:005619503Zawkwith the argumentprint $1will print the first argument... ie the output ofdate
-
Now lets remove the
T$ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F 'T' '{print $1}' > 2023-02-27 -
What happens when you do $2 on the same command as above?
Answer
> 12:50:48:341942600ZThis is because the input has been split and now
- $1 = 2023-02-27
- $2 = 12:20:40:005619503Z
-
Change the delimeter
-F 'T'to-F ':'and repeat both steps as before:Answer
$ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $1}' > 2023-02-27T12$ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $2}' 55 $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $3}' 32 $ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $4}' 473072761Z -
You can put these together using the print {$2$3$4$}, try.
sh date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F ':' '{print $2$3$4}' 5719707652042Z -
Experiment with this and consider piping with awk to see if you can get:
20230227130026355420256Answer
$ date +"%Y-%m-%dT%H:%M:%S:%NZ" | awk -F '[-T:Z]' '{print $1$2$3$4$5$6$7$8$9}' 20230227130026355420256