{"id":13749838,"url":"https://github.com/NisreenFarhoud/Bash-Cheatsheet","last_synced_at":"2025-05-09T13:30:56.187Z","repository":{"id":28903797,"uuid":"32428616","full_name":"NisreenFarhoud/Bash-Cheatsheet","owner":"NisreenFarhoud","description":null,"archived":false,"fork":false,"pushed_at":"2023-03-15T16:16:23.000Z","size":30,"stargazers_count":374,"open_issues_count":5,"forks_count":50,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-11-16T00:33:48.987Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NisreenFarhoud.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-03-18T00:34:15.000Z","updated_at":"2024-11-12T16:52:19.000Z","dependencies_parsed_at":"2024-01-18T09:56:55.608Z","dependency_job_id":"41616ec1-60a1-43c9-899a-97e9d583e091","html_url":"https://github.com/NisreenFarhoud/Bash-Cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NisreenFarhoud%2FBash-Cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NisreenFarhoud%2FBash-Cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NisreenFarhoud%2FBash-Cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NisreenFarhoud%2FBash-Cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NisreenFarhoud","download_url":"https://codeload.github.com/NisreenFarhoud/Bash-Cheatsheet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253258141,"owners_count":21879595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T07:01:14.687Z","updated_at":"2025-05-09T13:30:55.894Z","avatar_url":"https://github.com/NisreenFarhoud.png","language":null,"funding_links":[],"categories":["Others","Programming Languages"],"sub_categories":[],"readme":"Bash Shell Cheatsheet\n=====================\nThe main topics of this cheatsheet include an intro to the shell, navigating around the shell, common commands, environment variables, connectors, piping, I/O redirection, permissions, and keyboard shortcuts.\n## Introduction to the Shell\nThe shell is a program, in our case, called \"bash\" which stands for Bourne Again Shell.\nHow the shell works is it takes your commands and gives them to the operating system to perform.\nIn order to interact with the shell, we use \"terminal emulators\" such as the [gnome-terminal](https://help.gnome.org/users/gnome-terminal/stable/), [eterm](http://www.eterm.org/), nxterm, etc.\n## Navigating Around The Shell\nOn a Linux system, files are organized in a hierarchical directory structure.\nThis means there is a starting directory called the root directory.\nThis directory contains files and subdirectories that lead into other subdirectories.\n### pwd\nThe `pwd` command, short for print working directory, displays your current location in the directory structure.\n### cd\nThe `cd` command allows you to enter a new directory.\n\nSyntax | Explanation\n--- | ---\ncd | navigate to home directory\ncd ~ | navigate to home directory\ncd .. | navigate backwards to parent directory\ncd - | navigate to previous working directory\ncd Directory1 | navigate to directory named Directory1\ncd Directory1/Directory2 | navigate to directory, Directory2, through path\n### mkdir\nThe `mkdir` command makes a new directory in your current directory. \u003cbr\u003e\n## Common Commands\n### man\nThe `man` command directs you to the command manuals. \u003cbr\u003e\nFor example, the following command gives us all the information we need about the command `cat`. \u003cbr\u003e\n```\n  $ man cat\n```\n### cat\nThe `cat` command reads a file passed as a parameter and by default print its contents to standard output. \u003cbr\u003e\nPassing multiply files as parameters concatenates the files and then prints to standard output.\n### echo\nThe `echo` command prints its arguments to standard output. \u003cbr\u003e\n```\n  $ echo Hello World\n  Hello World\n```\nIf you call `echo` without any parameters, the command prints a new line.\n### head\nThe `head` command reads the first 10 lines of any passed in text and prints its contents to standard output.\nYou can change the default 10 lines to any number by manually passing in the desired size.\nFor example, the following prints all 50 lines of the file. \u003cbr\u003e\n```\n$ head -50 test.txt\n```\n### tail\nThe `tail` command reads the last 10 lines of any passed in text and prints its contents to standard output.\nYou can change the default 10 lines to any number by manually passing in the desired size.\nFor example, the following prints all 50 lines of the file. \u003cbr\u003e\n```\n  $ tail -50 test.txt\n```\nYou can also view in real time any text appended to the file with the `-f` flag.\n```\n$ tail -f test.txt\n```\n### less\nThe `less` command gives you a way to navigate through a passed file or block of text. Unlike the `more` command, less allows you to move backward through the file as well.\n```\n$ less test.txt\n$ ps aux | less\n```\nCommon less keyboard shortcuts | Description\n--- | ---\n  `G` | Moves to end of file\n  `g` | Moves to beginning of file\n  `:50` | Moves to the 50th line of the file\n  `q` | Exits less\n  `/searchterm` | Searches for any string matching 'searchterm' below the current line\n  `/` | Moves you to the next match for your previous 'searchterm' below the current line\n   `?searchterm` | Searches for any string matching 'searchterm' above the current line\n  `?` | Moves you to the next match for your previous 'searchterm' above the current line\n  `up` | Moves up a line\n  `down` | Moves down a line\n  `pageup` | Moves up a page\n  `pagedown` | Moves down a page\n### true\nThe `true` command always returns the exit status zero to indicate success.\n### false\nThe `false` command always returns the exit status non-zero to indicate failure.\n###`$?`\n`$?` is a variable that will return the exit code of the last command you ran.\n```\n$ true\n$ echo $?\n  0\n$ false\n$ echo $?\n  1\n ```\n### grep\nThe `grep` command is a search function. \u003cbr\u003e\nPassing a string and a file searches the file for the given string and prints the occurrences to standard output.\n```\n  $ cat users.txt\n  user:student password:123\n  user:teacher password:321\n  $ grep 'student` file1.txt\n  user:student password:123\n```\n`grep` can take multiple files as parameters and regular expressions to specify a pattern in text.\n\nCommon flags | Description\n--- | ---\n  `-i` | remove case sensitivity\n  `-r` | search recursively through directories\n  `-w` | search only whole words\n  `-c` | prints number of times found\n  `-n` | prints line found on with phrase\n  `-v` | prints invert match\n[See regex tutorial](https://github.com/mikeizbicki/ucr-cs100/tree/2015winter/textbook/using-bash/regex)\n### sed\nThe `sed` command is a stream editor that performs text transformations on an input. \u003cbr\u003e\nCommon use of this command is to replace expressions which takes the form `s/regexp/replacement/g`\nFor example, the following replaces all occurrences of the phrase \"Hello\" with \"Hi\".\n```\n  $ cat test.txt\n  Hello World\n  $ sed 's/Hello/Hi/g' test.txt\n  Hi World\n```\n[See sed tutorial](https://github.com/mikeizbicki/ucr-cs100/tree/2015winter/textbook/using-bash/sed)\n### history\nThe `history` command prints out an incremented command line history. \u003cbr\u003e\nIt is common to use the `grep` command with the `history` command in order to search for a particular command.\nFor example, the following searches your history for all occurrences of the string `g++`.\n```\n  $ history | grep g++\n  155  g++ file1.txt\n  159  g++ file2.txt\n```\n### export\nThe `export` command sets an environment variable to be passed to child processes in the environment. \u003cbr\u003e\nFor example, the following exports the variable \"name\" with the value \"student\". \u003cbr\u003e\n```\n  $ export name=student\n```\n### ps\nThe `ps` command, short for process status, prints out information about the processes running. \u003cbr\u003e\n```\n  $ ps\n  PID TTY          TIME CMD\n  35346 pts/2    00:00:00 bash\n```\nThere are four items displayed:\n  * process identification number (PID)\n  * terminal type (TTY),\n  * how long process has been running (TIME)\n  * name of command that launched the process (CMD)\n\n### awk\nThe `awk` command finds and replaces text by searching through files for lines that have a pattern. \u003cbr\u003e\nSyntax: `awk 'pattern {action}' test.txt`\n### wget\nThe `wget` command downloads files from the web and stores it in the current working directory. \u003cbr\u003e\n```\n  $ wget https://github.com/mikeizbicki/ucr-cs100\n```\n### nc\nThe `nc` command, short for netcat, is a utility used to debug and investigate the network. \u003cbr\u003e\n[See nc tutorial](https://github.com/mikeizbicki/ucr-cs100/tree/2015winter/textbook/using-bash/nctutorial)\n### ping\nThe `ping` command tests a network connection. \u003cbr\u003e\n```\n  $ ping google.com\n  PING google.com (74.125.224.34) 56(84) bytes of data.\n  64 bytes from lax17s01-in-f2.1e100.net (74.125.224.34): icmp_req=1 ttl=57 time=7.82 ms\n  --- google.com ping statistics ---\n  1 packets transmitted, 1 received, 0% packet loss, time 8ms\n  rtt min/avg/max/mdev = 7.794/8.422/10.792/0.699 ms\n```\nThe statistics at the end show an overview of how many connections went through before we called `^C` and how long it took.\n### git\n`Git` is a version control system that is commonly used in the industry and in open source projects. \u003cbr\u003e\n[See git tutorial](https://github.com/mikeizbicki/ucr-cs100/tree/2015winter/assignments/lab/lab1-git)\n## Environment Variables\nEnvironment variables are named variables that contain values used by one or more applications. \u003cbr\u003e\nThe `PATH` variable contains a list of directories where systems look for executable files. \u003cbr\u003e\nThe `HOME` variable contains the path to the home directory of the current user. \u003cbr\u003e\nThe `PS1` variable is the default prompt to control appearances of the command prompt. \u003cbr\u003e\n## Connectors\nConnectors allow you to run multiple commands at once.\n\nConnector | Description\n--- | --- | ---\n`\u0026\u0026` | first command always executes and the next command will only execute if the one before it succeeds\n`||` | first command always executes and the next command will only execute if the one before it fails\n`;` | first command and the following commands always execute\n```\n  $ true \u0026\u0026 echo Hello\n  Hello\n  $ false || echo Hello\n  Hello\n  $ echo Hello ; ls\n  Hello\n  test.txt file1.txt file2.txt\n```\n## Piping\nPipes connect multiple commands together by sending the stdout of the first command to the stdin of the next command.\nFor example, the following sends the `ls` output to `head` so that only the top 10 items get printed. \u003cbr\u003e\n```\n  $ ls -l | head\n```\n## Input/Output Redirection\n### Output Redirection\nStandard output redirection uses the symbols `\u003e` and `\u003e\u003e`. \u003cbr\u003e\nFor example, the following sends the output of `ls` into the file instead of printing to the screen.\n```\n  ls \u003e files.txt\n  $ cat files.txt\n  file1.cpp sample.txt\n```\nIf the file isn't already in your working directory, the file gets created.\nIf the file already exists, then the contents of the command overwrites what is already in the file. \u003cbr\u003e\nTo avoid overwriting a file, the `\u003e\u003e` command appends to the end of the file instead.\n### Input Redirection\nStandard input redirection uses the symbol `\u003c`. \u003cbr\u003e\nFor example, the following causes `sort` to access its input from the file instead of the keyboard.\n```\n  $ cat files.txt\n  c\n  b\n  $ sort \u003c files.txt\n  b\n  c\n```\nThe `sort` command prints the contents of the file and prints to the screen because we haven't redirected its output.\nBut we can combine I/O redirection into one command line, such as:\n```\n  $ sort \u003c files.txt \u003e files_sorted.txt\n```\n### Advanced Redirection\nAdding a `\u0026` with the `\u003e` symbol results in redirecting both standard out and standard error.\nFor example, the `test.cpp` file prints the string \"stdout\" with `cout` and the string \"stderr\" with `cerr`.\n```\n  $ g++ test.cpp\n  $ ./a.out \u003e\u0026 test.txt\n  $ cat test.txt\n  stdout\n  stderr\n```\nThe `\u003e` symbol alone only redirects standard output. \u003cbr\u003e\nIf you only want to redirect a specific file descriptor you can attach the file descriptor number to `\u003e`.\n\nName | File Descriptor | Description\n--- | --- | ---\nstdin | 0 | standard input stream\nstdout | 1 | standard output stream\nstderr | 2 | standard error output stream\nFor example, if I only wanted to redirect \"stderr\" to the file `test.txt` from the above example, I would do the following:\n```\n  $ g++ test.cpp\n  $ ./a.out 2\u003e test.txt\n  stdout\n  $ cat test.txt\n  stderr\n```\n## Permissions\nThe command `ls -l` prints out a lot of information about each file that is informative about the permissions.\n```\n  $ ls -l test.txt\n  -rw-rw-r--  1  user  group  1097374 January 26 2:48 test.txt\n```\n\nOutput from example above | Description/Possible Outputs\n--- | ---\n- | File type: \u003cbr\u003e `-`= regular file \u003cbr\u003e `d`= directory\nrw- | Permissions for owner of file\nrw- | Permissions for members of the group owning the file\nr-- | Permissions for all other users\nuser | name of user owning the file\ngroup | name of group owning the file\n### chmod\nThe `chmod` command, short for change mode, changes the permissions of a file. \u003cbr\u003e\nThere is a combination of letters that need to be known in order to change specific users' permission.\n\nLetter | User\n--- | ---\nu | User who owns it\ng | Users in the group\no | Other users not in the group\na | All users\nYou call `chmod` by describing which actions you want to perform and to which file.\nThe `-` symbol represents taking away permissions while the `+` symbol represents adding permissions.\nThe following example makes the file readable and writable to the user who owns it and the group.\n```\n  $ chmod ug+rw test.txt\n  $ ls -l test.txt\n  -rw-rw----  1  user  group  1097374 January 26 2:48 test.txt\n```\nAlternatively, we can use `chmod` with hex numbers.\nYou can think of each permission setting as a bit where it is a `1` if there is permission for the file and `0` otherwise.\n```\n  rwx = 111 = 7\n  rw- = 110 = 6\n  r-x = 101 = 5\n  r-- = 100 = 4\n```\nEach set of permissions represents a single digit so the following commands have the same outcome as above.\n```\n  $ chmod 660 test.txt\n```\n[See permissions tutorial](https://github.com/mikeizbicki/ucr-cs100/tree/2015winter/textbook/using-bash/file-permission)\n## Keyboard Shortcuts\nShortcut | Description\n--- | ---\nCTRL-A | Move cursor to beginning of line\nCTRL-E | Move cursor to end of line\nCTRL-R | Search bash history\nCTRL-W | Cut the last word\nCTRL-U | Cut everything before the cursor\nCTRL-K | Cut everything after the cursor\nCTRL-Y | Paste the last thing to be cut\nCTRL-_ | Undo\nCTRL-L | Clears terminal screen\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNisreenFarhoud%2FBash-Cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FNisreenFarhoud%2FBash-Cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FNisreenFarhoud%2FBash-Cheatsheet/lists"}