Chat Zalo Chat Messenger Phone Number Đăng nhập
Bash cheat sheet: Top 25 commands and creating custom commands

Bash cheat sheet: Top 25 commands and creating custom commands

Top 25 Bash

Commands Quick note: Anything enclosed in [ ] means it’s optional. Some commands can be used without options or specifying files.

ls

— Enumerating the contents of the ls directory

is probably the most common command. Many times, you’ll be working in a directory and need to know what files are located there. The ls command allows you to quickly view all files within the specified directory.

  • Syntax: ls [option(s)] [file(s)]

  • Common options: -a, -l

echo — Prints text in the terminal window

echo prints

text in

the terminal window and is typically used in shell scripts and batch files to send status text to the screen or to a computer file. Echo is also particularly useful for displaying the values of environmental variables, which tell the shell how to behave when a user works on the command line or in scripts.

Syntax: echo [option(s)]

  • [string(s)]
  • Common options: -e, -n

touch — Creating a file tap

is going to be the easiest way to create new files, but it can also be used to change timestamps on files and/or directories. You can create as many files as you want in a single command without worrying about overwriting files with the same name.

  • Syntax: tap [option(s)] file_name(s)

  • Common options: -a, -m, –

  • r, -d

mkdir

— Create a directory mkdir

is a useful command that you can use to create directories. Any number of directories can be created simultaneously, which can greatly speed up the process.

  • Syntax: mkdir [option(s)] directory_name(s)

  • Common options: -m, -p, -v

grep

— search grep is

used to search text for user-specified patterns. It is one of the most useful and powerful commands. There are often scenarios where you will be tasked with finding a particular string or pattern within a file, but you don’t know where to start looking, that’s where grep comes in extremely useful.

  • Syntax: grep [option(s)] pattern [file(s)]

  • Common options: -i, -c, -n

man

— Print manual or get help for

a command

The man command is your manual and is very useful when you need to figure out what a command does. For example, if you didn’t know what the rmdir command does, you could use the man command to find out.

  • Syntax: man [option(s)] keyword(s)

  • Common options: -w, -f, -b

pwd

— Print working directory pwd

is

used to print the current directory in which it is located. As an example, if you have several terminals running and need to remember the exact directory you are working on, then pwd will tell you.

Syntax: pwd [option(s)] Common options: options are not normally used with pwd cd —

Change

directory cd will change the directory

you are in so that you can get information, manipulate, read, etc. the different files and directories on your system.

  • Syntax: cd [option(s)]

  • directory

  • Common options: options are not normally used with cd

mv — Move or rename the directory

VM is used to move or rename directories. Without this command, you would have to individually rename each file which is tedious. MV allows you to rename files in batches, which can save you a lot of time.

  • Syntax: mv [option(s)] argument(s)

  • Common options: -i, -b

rmdir

— Deleting rmdir directory

will delete empty directories. This can help clean up space on your computer and keep files and folders organized. It is important to note that there are two ways to delete directories: rm and rmdir. The distinction between the two is that rmdir will only delete empty directories, while rm will delete directories and files regardless of whether they contain data or not.

  • Syntax: rmdir [option(s)] directory_names

  • Common options: -p

locate — Locate

a specific file or directory

This is by far the easiest way to find a file or directory. You can keep your search broad if you don’t know what exactly you’re looking for, or you can narrow the scope by using wildcards or regular expressions.

  • Syntax: locate [option(s)] file_name(s)

  • Common options: -q, -n, -i

Do you like the article? Scroll down to subscribe to our free bi-monthly newsletter.

less: view the contents of a text file

The less command allows you to view files without opening an editor. It is faster to use and there is no chance for you to inadvertently modify the file.

  • Syntax: less file_name

  • Common options: -e, -f, -n compgen — Displays all available commands, aliases, and functions compgen is a useful command when you need to

  • reference all available commands, aliases,

and functions

.

  • Syntax: compgen [option(s)]

  • Common options: -a, -c, -d

> — redirect stdout

The > character is the redirect operator. This takes the output of the previous command that you would normally see in the terminal and sends it to a file that you give it. As an example, echo “contents of file1” > file1. Here you create a file called file1 and place the echo string in it.

  • Syntax: >

  • Common options: n/a cat — Read a file, create a file, and concatenate cat files is one of the most versatile commands and serves three main functions: display them, merge copies of them, and

  • create

new ones.

Syntax: cat [option(s)] [file_name(s)] [-

  • ] [file_name(s)]

  • Common options: -n | — Pipe A pipeline takes the standard output of one command and passes it as input to another. Syntax: |

  • Common options

  • : n/

a head

Read the start of a file by default the head

command displays the

first 10 lines

of a file

. There are times when you may need to quickly look at a few lines in a file and head allows you to do so. A typical example of when you want to use head is when you need to analyze frequently changing records or text files.

  • Syntax: head [option(s)] file(s)

  • Common options: -n

tail — Read the end of a file By default, the tail command displays the last 10 lines

of a file

. There are times when you may need to quickly look at a few lines in a file and the queue allows you to do so. A typical example of when you’d want to use tail is when you need to analyze records or text files that change frequently.

  • Syntax: tail file_names

  • Common options: -n

chmod — Sets the file permissions flag

on a file or folder

There are situations where you or a colleague will try to upload a file or modify a document and receive an error because you don’t have access. The quick fix for this is to use chmod. Permissions can be set with alphanumeric characters (u, g, o) and assigned their access with w, r, x. Conversely, you can also use octal numbers (0-7) to change permissions. For example, chmod 777 will my_file give everyone access.

Syntax:

  • chmod [option(s)] permissions file_name

  • Common options: –

  • f, -v

exit

— Exit

a directory The exit command will close a

terminal window, finish running a shell script, or close the SSH remote access session.

  • Syntax: exit

  • Common options: n/a

Contact US