Shell

BASH Commands :

#bash

Bash works in a REPL way --> Read, Eval, Print, Loop

Special Commands What does it do? Special cases: Examples with flags: Examples:
pwd Print working directory pwd
ls list (lists the contents of a directory) ls -a
List all content

ls -l
(for long listing) The option displays metadata about your Linux files, including the permissions set on each file.
ls /Sohag/home
cd current directory
(Change where you are in the file system.)
current directory - cd .. Goes one level up.
- cd . goes one level down.
- cd ~ goes back to home
wc word count wc -l example.txt
cp copy cp example.txt example_copy.txt
mv Move file or rename file
mv example_copy.txt experiment/
rm Remove file rm -i file*
rm Rename file There is no Terminal command to rename directly, we move file. It is very easy to overwrite files; be careful! mv file1 fileRename
head / tail Shows the first o last line.
head prints from the start of the file
tail prints from the end of the file.
-n Allows for determining which line to start from.
head -n3 data
tail n3 data
cat Prints out the content of a file, and output it into the terminal I personally use bat which is more redeable. - cat file, which prints the contents of file.
- sort file, which prints out the lines of file in sorted order.
- uniq file, which eliminates consecutive duplicate lines from file.
- head file and tail file, which respectively print the first and last few lines of file.
sort Take an input and sort in lexicographical order. sort -u
Eliminate duplicates, sort unique values.
grep File searcher, which finds lines matching pattern in file
Can also search directories.
It's a regular expression.



pass -r to recursively search all the files in a directory.



ˆ Starts with pattern

$ Ends with pattern


-A1 Line after pattern

-B1 Line before pattern

-C1 Provide context


grep 3 data


grep 'ab' testo1.txt Search a file or an input stream
for a sort of pattern.


grep 'ˆab' testo.txt This search begins with a pattern.
grep 'ab$' testo.txt This search something that ends in that pattern.

grep -A1 'ab' testo1.txt Every time a pattern shows up, it finds one line after it.

grep -B1 'ab' testo1.txt Every time a pattern shows up, it finds one line before it.

grep -C1 'ab' testo1.txt Every time a pattern shows up, we find one line of context
find Search for files that match a specific file system structure. Find is recursive by default -type f Specifies is type file.
-size Specifies size
-exec Execute the commands
find ~/Downloads -type f -name "*.zip" -mtime +30
Finds ZIP files in the download directory that are older than 30 days.
-------------------------
find ~/Downloads -type f -size +100M -exec ls -lh {} \ Find all the files that are 100 m long, list them through ls, and print them in a list of human-readable lines.
---------------------------
find . -name "*.md" -exec grep -l "TODO" {} Find all the markdown files in the current directory and for each one it will execute a grep search for the "todo " line, and list them by line.
Note that -exec takes a command terminated with a stand-alone ; (which we need to escape much like a space) where {} is replaced with each matching file path by find.
touch Creates a file or Update files touch .hidden.txt

touch {file1, file2, file3}.txt
touch test.py Create single files

touch {test1,test2,test3}.py Creates multiple .py files in one line.

touch .secret.txt Creates a hidden visible not visible in Desktop.
alias Create aliaes for: files, commands `alias ls= find

Command line environment

This is specific to the shell environment.

Arguments :

  • Arguments have flags to specify the criteria of a command
  • Arguments can have multiple input
  • Flags can have a single - or -- (This is used for convention)
  • -a and --all are the same options
  • Can have multiple flags in a single line.

Bash PATH resolution

$PATH is a built-in variable that checks files, in a specific order, in directories

Command Explanation Flags
$PATH attachments/Screenshot 2025-12-28 at 12.54.06.png
echo $PATH | tr : '\n' This command put them in order tr stands for translate command, and /n means newline.

Bash basics

Searching files

Bash process exists inside User Directory --> pwd

Commands Purpose Attention Flags Example
cat text1.txt Read file content
grep 'ab' testo1.txt Search a file or input stream
for a sort of pattern.
grep 'ˆab' testo.txt This search something that begins in a pattern ˆ
grep 'ab$' testo.txt This search something that ends in that pattern $
grep -A1 'ab' testo1.txt Every time a pattern shows up, we find one line
after it.
1 stands for 1 line, can be 2,3,4,.. -A1 Line after
grep -B1 'ab' testo1.txt Every time a pattern shows up, we find one line
before it
-B1 Line before
grep -C1 'ab' testo1.txt Every time a pattern shows up, we find one line context -C1 Provide context

Bash basics

Type, File,

type commands find the different commands that are built-in in the shell vs in the system

file command shows the type of file in the cd

Commands Purpose Example
type echo Will show where the commands path is ls is /bin/ls
type history will show that it is built-in history is a shell builtin
file nameFile.txt This shows type of file testo1.txt: ASCII text

Environment variables

foo=bar

To define a variable in BASH --> name=sohag
  • Cannot use space as the program take it as 2 arguments:
    -name = sohag This run program name with argument = and sohag which gives back error.

Loops:

For loop

for i in $(seq 1 10); do
This executes the command seq 1 10 (which prints the numbers from 1 to 10 inclusive) and then replaces the whole $() with that command’s output, giving you a 10-iteration for loop. In older code you’ll sometimes see literal backticks (like for i in `seq 1 10`; do) instead of $(), but you should strongly prefer the $() form as it can be nested.

Redirectors or Pipes |

A pipe (|) is used to connect multiple commands. It's a construct of the BASH programming language.

It takes the output from the previous command and “pipes” it into the input of the following command. (Run the left program, take its output, and use it as input of the program at the right.)

>file lets you take the standard output of a program and write it to file instead of to your terminal. This makes it easier to analyze after the fact. >>file will append to file rather than overwrite it. There’s also <file which tells the shell to read from file instead of from your keyboard as the standard input to a program.

Command What does it do? Example Flags Edge cases:
date prints out the date date > todayDate.txt
<> Ri-directs the output as input to the next file date > time.txt It overwrites the file
>> Append to the file date >> time.txt

Bash scripting rules

Single vs Double Quotes | [] vs

Command Explanation Flags Examples Notes
'' vs "" - Use single quotes when you want exact text

- Use double quotes when you want variables to expand
echo 'This is Sohag'

name=Sohag


echo "This is Sohag"
(()) Double parenthesis in a for loop tells BASH we are in a math mode
(( )) is Bash’s arithmetic command. Everything inside is treated as integer math, not as strings.
Handles Math
[[]] [[ condition ]] is Bash keyword syntax, not a command. [[ condition ]] && action -e testFlags && echo exists Because it’s a command:

- Spaces are mandatory

- Variables must be expanded ($a)

- It does not have to be inside a script, we can run it.

Blackshash and dash

/ This is called an escape character that tells the shell in Bash to do not consider the next character.
- Dash alone is an input , not a flag

Powered by Forestry.md