shell I/O

Shell Input/Output (I/O) refers to the way a shell interacts with data streams—taking in input, processing it, and sending output. In Unix-like systems, shell I/O management revolves around three main data streams: standard input (stdin), standard output (stdout), and standard error (stderr). These streams are key to handling how commands receive, process, and return data.

Shell I/O Components:

Standard Input (stdin) Descriptor: 0 Purpose: Used for receiving input data. By default, stdin reads from the keyboard or terminal input, but it can be redirected to read from files or other input sources. Standard Output (stdout) Descriptor: 1 Purpose: Used for sending normal output. By default, stdout sends output to the terminal, but it can be redirected to files or other programs. Standard Error (stderr) Descriptor: 2 Purpose: Used for sending error messages. By default, stderr also sends output to the terminal but can be redirected separately from stdout to keep errors and regular output distinct.

cat                 # prints a file to stdout
cat -b              # prints non-blank numbered lines
cut -d <separator> -f <col NO> /etc/passwd
# separate a file with separator and send specified column
echo                # shows a message or variable
less                # show stdout in a nice way, it should be piped after other commands
cmd1 ; cmd2 ; cmd3  # run multiple commands 
cmd1 && cmd2        # cmd2 runs if cmd1 is successful
cmd1 || cmd2        # cmd2 runs if cmd1 does NOT run successfully
cmd  > filename     # send output to filename (overwrite)
cmd >> filename     # append output to filename
cmd < filename      # send input data to cmd from filename
wc                  # print newline, word, and byte counts from std input or file (-l, -c, -w)
grep -i             # find something from std input or file (-i for ignore case)
uniq                # report or omit repeated lines
tr                  # translate, squeeze, and/or delete characters from standard input

# Example
# $ sort < temp.txt | uniq -c | sort -nr
#  5 mina
#  3 ali
#  2 sara
#  2 reza
#  2 keyvan
#  1 jafar

sed 's/old/new/'    # it replaces the first old word of each line with new like vim
sed 's/old/new/g'   # it replaces globally
sed -E 's/old/new/' # it supports regex

Shell Commands

# Directories starting with / are called absolute, and those without / are called relative
# A filename in Linux consists of the full path (dirname + basename)
uname -a            # display Linux system info
hostname            # display hostname (it shows IP with the -i switch)
last reboot         # it can show more things like last {reboot, syslog, ...}
whoami              # returns the current logged-in user
basename FILE       # returns the base name of the file
dirname FILE        # returns the directory name of the file
du -ha              # shows disk usage by files in human-readable format
whereis             # shows the binary file of a command
which               # shows where the executable file is located
head                # displays the first lines of a file (default is 10 lines)
tail                # displays the last lines of a file (default is 10 lines)
tail -f             # displays the last lines of a file in live mode (updates with changes)
ls -ltrha           # list all files and folders, time sorted, reversed, human-readable mode
mkdir -p            # make directories with their parent directories
cp -r               # copy folders and their files recursively
stat filename       # view inode details
dd                  # make a byte-by-byte image to a file
time CMD            # calculate time for processing CMD
date                # returns the current date and time
date +"%Y%m%d%H%M"  # returns the date and time formatted
cal                 # shows a calendar
id                  # shows the active user ID with login and group
who                 # shows who is logged on the system
w                   # like who, with more details
users               # lists logged-in users
pwd                 # returns the current directory
touch               # create a file or update the access time of a file
gpg -c FILE         # encrypt a file
gpg FILE.gpg        # decrypt a file
ssh <username>@<host>  -p1234   # connect to a remote host via SSH on port 1234
scp SOURCE_FILE user@host:DEST_FILE  # securely copy a file to a remote host