{"id":18034705,"url":"https://github.com/yuanqing/shell-basics","last_synced_at":"2025-10-04T02:30:26.210Z","repository":{"id":25913890,"uuid":"29354762","full_name":"yuanqing/shell-basics","owner":"yuanqing","description":":shell: A quick overview of the Unix shell","archived":false,"fork":false,"pushed_at":"2021-01-30T07:41:30.000Z","size":6,"stargazers_count":12,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-17T23:40:24.667Z","etag":null,"topics":["cheatsheet","shell","terminal"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuanqing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-16T15:41:20.000Z","updated_at":"2024-07-08T02:32:35.000Z","dependencies_parsed_at":"2022-07-24T07:46:03.656Z","dependency_job_id":null,"html_url":"https://github.com/yuanqing/shell-basics","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/yuanqing%2Fshell-basics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fshell-basics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fshell-basics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fshell-basics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuanqing","download_url":"https://codeload.github.com/yuanqing/shell-basics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235209003,"owners_count":18953003,"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":["cheatsheet","shell","terminal"],"created_at":"2024-10-30T11:13:28.739Z","updated_at":"2025-10-04T02:30:20.923Z","avatar_url":"https://github.com/yuanqing.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shell Basics\n\n\u003e A quick overview of the Unix shell\n\n- [Introduction](#introduction)\n- [Commands](#commands)\n- [Paths](#paths)\n- [I/O redirection](#io-redirection)\n- [Shell scripts](#shell-scripts)\n- [The `\u003cCtrl\u003e` + `z` “problem”](#the-ctrl--z-problem)\n- [See also](#see-also)\n\n## Introduction\n\nThe Unix shell (or *terminal*, or *command line*) is a program that allows us to run other programs via a text interface.\n\n### Running a program\n\nTo run a program, type its name and hit `\u003cEnter\u003e`. For example, the `pwd` program is for printing the path of the current directory.\n\n```sh\n$ pwd\n/home/y/yuanqing\n```\n\n(In all examples shown, a command is indicated with a `$` prefix. Do not type the `$`.)\n\n### Passing arguments to a program\n\nSome programs accept arguments, which come after the name of the program. For example, the `cd` program is for changing the current directory:\n\n```sh\n$ cd foo\n$ pwd\n/home/y/yuanqing/foo\n```\n\n## Commands\n\nListed here are the more important command line operations that you should know.\n\nOperation | Command\n:--|:--\nShow the path of the current directory | `pwd`\nList all files and directories in the current directory | `ls -a`\nList all files and directories in the current directory, excluding hidden files | `ls`\nChange the current directory to the specified directory | `cd foo`\nChange the current directory to the home directory | `cd`\nChange the current directory to the parent directory | `cd ..`\nChange the current directory to the previous directory | `cd -`\nShow the contents of a file | `cat foo`\nPrint a string | `echo 'foo'`\nCreate an empty file | `touch foo`\nCreate a file containing the specified string | `echo 'foo' \u003e bar`\nCreate an empty directory | `mkdir foo`\nCopy a file | `cp foo bar`\nCopy a file at the specified path to the current directory | `cp ~/foo/bar .`\nCopy a directory | `cp -r foo bar/`\nDelete a file | `rm foo`\nDelete a directory | `rm -rf foo`\nMove/rename a file/directory | `mv foo bar`\nCompare the contents of two files | `diff foo bar`\nClear the terminal screen | `clear`\nShow your command history | `history`\nOpen the user manual for a command | `man foo`\nWrite the output of a program to a file | `java HelloWorld \u003e foo`\nWrite the output of a program to a file, appending to the end of the specified file | `java HelloWorld \u003e\u003e foo`\nUse the contents of a file as the input to a program | `java Hello \u003c input`\nUse the contents of a file as the input to a program, and write its output to a file | `java Hello \u003c input \u003e output`\nRun the shell commands listed in the specified text file | `bash foo`\nBring the most recent “backgrounded” program back to the foreground | `fg`\n\nBefore hitting `\u003cEnter\u003e`, you can:\n\n1. Press `\u003cCtrl\u003e` + `c` at any time to discard the command.\n2. Use the \u003ccode\u003e\u0026uarr;\u003c/code\u003e and \u003ccode\u003e\u0026darr;\u003c/code\u003e arrow keys to toggle through your command history.\n3. Use the `\u003cTab\u003e` key to autocomplete commands.\n\nTo terminate a running program, press `\u003cCtrl\u003e` + `d`. (See [the `\u003cCtrl\u003e` + `z` “problem”](#the-ctrl--z-problem).)\n\n## Paths\n\nMany programs take files/directories as arguments. A file/directory path can be specified as follows:\n\nFile/directory | Path\n:--|:--\nThe current directory | `.`\nThe parent directory | `..`\nThe directory two levels up | `../..`\nThe home directory | `~`\nThe root directory | `/`\nA file/directory in the current directory | `foo` or `./foo`\nA file/directory in the parent directory | `../foo`\nA file/directory in the directory two levels up | `../../foo`\nA file/directory in the home directory | `~/foo`\nA file/directory in the root directory | `/foo`\n\n## I/O redirection\n\n### Redirect output\n\nThe output of a program is typically displayed on the terminal (ie. `stdout`):\n\n```sh\n$ javac HelloWorld.java\n$ java HelloWorld\nhello world\n```\n\nUse the `\u003e` operator to write the program\u0026rsquo;s output to a file:\n\n```sh\n$ java HelloWorld \u003e foo\n$ cat foo\nhello world\n```\n\nIf you use the `\u003e` operator, the specified file will be *overwritten*, so be careful! To merely *append* the output to the end of the specified file, use the `\u003e\u003e` operator instead:\n\n```sh\n$ java HelloWorld \u003e\u003e bar\n$ cat bar\nhello world\n$ java HelloWorld \u003e\u003e bar\n$ cat bar\nhello world\nhello world\n```\n\n### Redirect input\n\nMany programs accept input from the keyboard (ie. `stdin`):\n\n```sh\n$ javac Hello.java\n$ java Hello\nfoo\nhello foo\n```\n\n(Here, [Hello](https://gist.github.com/yuanqing/6bc1da264cd7ba108d8325d7b49ad2b3) is a toy Java program that accepts a string, then outputs \u003ccode\u003ehello\u0026nbsp;\u003c/code\u003e followed by the string that was entered.)\n\nWe can use the contents of a file as the input to a program via the `\u003c` operator:\n\n```sh\n$ cat input\nfoo\n$ java Hello \u003c input\nhello foo\n```\n\n### Redirect input and output\n\nWe can redirect both input and output in a single command:\n\n```sh\n$ javac Hello.java\n$ cat input\nfoo\n$ java Hello \u003c input \u003e output\n$ cat output\nhello foo\n```\n\n## Shell scripts\n\nA shell script is simply a sequence of commands listed in a text file.\n\nSuppose we have a text file named `commands` containing the following:\n\n```sh\njavac Hello.java\ncat input\njava Hello \u003c input \u003e output\ncat output\n```\n\nWe can run the commands listed in the `commands` text file using the `bash` program:\n\n```sh\n$ bash commands\nfoo\nhello foo\n```\n\n## The `\u003cCtrl\u003e` + `z` “problem”\n\nIf you\u0026rsquo;d pressed `\u003cCtrl\u003e` + `z` while a program was still running, you would see something like the following:\n\n```sh\n$ javac Hello.java\n$ java Hello\n^Z\n[1]+  Stopped                 java Hello\n```\n\nA quick fix is to run the `fg` program:\n\n```sh\n$ fg\n```\n\nThis brings us back to our running program, and all is well with the world.\n\n(Explanation: Pressing `\u003cCtrl\u003e` + `z` [places the currently-running program in the background](http://en.wikibooks.org/wiki/A_Quick_Introduction_to_Unix/Job_Control#Controlling_Jobs_in_Unix). The `fg` program simply brings the most recent “backgrounded” program back to the foreground.)\n\n## See also\n\n- [Vim Basics](https://github.com/yuanqing/vim-basics/blob/master/README.md#readme)\n- [Unix Workshop](https://github.com/cyberwizardinstitute/workshops/blob/master/unix.md#readme)\n- [The Unix Philosophy](http://www.faqs.org/docs/artu/ch01s06.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuanqing%2Fshell-basics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuanqing%2Fshell-basics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuanqing%2Fshell-basics/lists"}