Beginner Guide Using Bash
What is Bash? is a command-line interpreter that lets you interact with your UNIX or Apple OS.
Bash is useful for navigating into your system, run programs and automate tasks. For many environments—especially remote servers accessed via SSH—Bash is the main way to interact with the system, since there’s no graphical interface (GUI).
Basic Bash Commands
Let’s list some of the most common commands you’ll use almost every day
Basic Navigation Commands
pwd # Show current directory
ls # List files
cd # Change directory
cd .. # Go up one level
cd ~ # Go to home directory
cd - # Go to the previous directoryExample
cd /var/log
pwd
# /var/log
cd ..
# /var
ls
# apache.log wifi.log system.logWorking with Files & Folders
mkdir project # Create folder
touch app.py # Create file
cp file.txt copy.txt # Copy file
mv file.txt new.txt # Move/rename
rm file.txt # Delete file
rm -r folder/ # Delete folderExample
mkdir project
touch app.py
mv app.py project/app.py
rm project/app.py or rm -r project #to remove both folder and file⚠️ Be very careful with rm — there is no undo.
What is sudo? sudo (superuser do) allows you to run commands with administrator privileges. In simple terms, it removes most safety restrictions. That’s why sudo rm -r is so dangerous — it can delete anything on the system without asking twice and it will be "Game Over".
Viewing File Content
cat file.txt # Show full content
less file.txt # Scroll through file
head file.txt # First lines
tail file.txt # Last lines
tail -f app.log # Follow live updates of file contentExample
cat file.txt
# This File is a test fileSearching
grep "error" app.log # find lines inside the file containing the work "error"
find . -name "*.log" # find files by namePermissions
chmod +x script.sh #Makes the script executableCombining Commands
cat file.txt | grep "error" # take the output of one command and pass it to another
echo "This is a test" >> file.txt # append to file
echo "This is a test" > file.txt # overwrite fileSimple Scripting
touch script.sh
chmod +x script.sh
vim script.sh or nano script.sh
Inside the file with your favorite text editor
#!/bin/bash
# printing this sentence in the terminal. Why not?
echo "Lets write my first script"
#Create a file
touch file.txt
#Insert the text inside the file
echo "I created this file" > file.txt
#Show the context of the file
cat file.txtExecute the script
./script.shOutput
Lets write my first script
I created this fileTips
- Every command has options (arguments) that change its behavior.
- Every argument is useful, it will depend on the situation or what you are trying to accomplish with it.
- You can read the manual for any command:
man ls
NAME
ls – list directory contents
SYNOPSIS
ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]
DESCRIPTION
For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each operand
that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, associated information.
If no operands are given, the contents of the current directory are displayed. If more than one operand is given, non-directory operands are displayed
first; directory and non-directory operands are sorted separately and in lexicographical order.
The following options are available:
-@ Display extended attribute keys and sizes in long (-l) output.
-A Include directory entries whose names begin with a dot (‘.’) except for . and ... Automatically set for the super-user unless -I is specified.
-B Force printing of non-printable characters (as defined by ctype(3) and current locale settings) in file names as \xxx, where xxx is the numeric
value of the character in octal. This option is not defined in IEEE Std 1003.1-2008 (“POSIX.1”).
-C Force multi-column output; this is the default when output is to a terminal.
-D format
When printing in the long (-l) format, use format to format the date and time output. The argument format is a string used by strftime(3).
Depending on the choice of format string, this may result in a different number of columns in the output. This option overrides the -T option.
This option is not defined in IEEE Std 1003.1-2008 (“POSIX.1”).
-F Display a slash (‘/’) immediately after each pathname that is a directory, an asterisk (‘*’) after each that is executable, an at sign (‘@’) after
each symbolic link, an equals sign (‘=’) after each socket, a percent sign (‘%’) after each whiteout, and a vertical bar (‘|’) after each that is a
FIFO.
man cat
NAME
cat – concatenate and print files
SYNOPSIS
cat [-belnstuv] [file ...]
DESCRIPTION
The cat utility reads files sequentially, writing them to the standard output. The file operands are processed in command-line order. If file is a single
dash (‘-’) or absent, cat reads from the standard input. If file is a UNIX domain socket, cat connects to it and then reads it until EOF. This complements
the UNIX domain binding capability available in inetd(8).
The options are as follows:
-b Number the non-blank output lines, starting at 1.
-e Display non-printing characters (see the -v option), and display a dollar sign (‘$’) at the end of each line.
-l Set an exclusive advisory lock on the standard output file descriptor. This lock is set using fcntl(2) with the F_SETLKW command. If the output
file is already locked, cat will block until the lock is acquired.
-n Number the output lines, starting at 1.
-s Squeeze multiple adjacent empty lines, causing the output to be single spaced.
-t Display non-printing characters (see the -v option), and display tab characters as ‘^I’.
-u Disable output buffering.
-v Display non-printing characters so they are visible. Control characters print as ‘^X’ for control-X; the delete character (octal 0177) prints as
‘^?’. Non-ASCII characters (with the high bit set) are printed as ‘M-’ (for meta) followed by the character for the low 7 bits.
Practice makes perfect. Don’t try to learn everything at once—with time, it will feel natural.
For more information about bash and editing files check this post.