BASH Commands :
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 -aList 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 filetail prints from the end of the file. | -n Allows for determining which line to start from. | head -n3 datatail 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 -uEliminate duplicates, sort unique values. | ||
| grep | File searcher, which finds lines matching pattern in fileCan 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 datagrep '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 +30Finds 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.txttouch {file1, file2, file3}.txt | touch test.py Create single filestouch {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 |
