Bash Commands Flashcards
ls
ls — List directory contents
ls is probably the most common command. A lot of times, you’ll be working in a directory and you’ll 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
echo — Prints text to the terminal window
echo prints text to the terminal window and is typically used in shell scripts and batch files to output status text to the screen or a computer file. Echo is also particularly useful for showing the values of environmental variables, which tell the shell how to behave as a user works at the command line or in scripts.
Syntax: echo [option(s)] [string(s)]
Common options: -e,
touch
touch — Creates a file
touch 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: touch [option(s)] file_name(s)
Common options: -a, -m, -r, -d
mkdir
mkdir — Create a directory
mkdir is a useful command 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
grep — search
grep is used to search text for patterns specified by the user. It is one of the most useful and powerful commands. There are often scenarios where you’ll be tasked to find a particular string or pattern within a file, but you don’t know where to start looking, that is where grep is extremely useful.
Syntax: grep [option(s)] pattern [file(s)]
Common options: -i, -c, -n
man
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 command rmdir does, you could use the man command to find that out.
Syntax: man [option(s)] keyword(s)
Common options: -w, -f, -b
pwd
pwd — Print working directory
pwd is used to print the current directory you’re in. As an example, if you have multiple terminals going and you need to remember the exact directory you’re working within, then pwd will tell you.
Syntax: pwd [option(s)]
Common options: options aren’t typically used with pwd
cd
cd — Change directory
cd will change the directory you’re in so that you can get info, manipulate, read, etc. the different files and directories in your system.
Syntax: cd [option(s)] directory
Common options: options aren’t typically used with cd
mv
mv — Move or rename directory
mv 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 do batch file renaming which can save you loads of time.
Syntax: mv [option(s)] argument(s)
Common options: -i, -b
rmdir
rmdir — Remove directory
rmdir will remove empty directories. This can help clean up space on your computer and keep files and folders organized. It’s important to note that there are two ways to remove directories: rm and rmdir. The distinction between the two is that rmdir will only delete empty directories, whereas rm will remove directories and files regardless if they contain data or not.
Syntax: rmdir [option(s)] directory_names
Common options: -p
locate
locate — Locate a specific file or directory
This is by far the simplest way to find a file or directory. You can keep your search broad if you don’t know what exactly it is 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
less
less — view the contents of a text file
The less command allows you to view files without opening an editor. It’s faster to use, and there’s no chance of you inadvertently modifying the file.
Syntax: less file_name
Common options: -e, -f, -n
compgen
compgen — Shows all available commands, aliases, and functions
compgen is a handy command when you need to reference all available commands, aliases, and functions.
Syntax: compgen [option(s)]
Common options: -a, -c, -d
> — redirect stdout
> — redirect stdout
The > character is the redirect operator. This takes the output from the preceding command that you’d normally see in the terminal and sends it to a file that you give it. As an example, take echo “contents of file1” > file1. Here it creates a file called file1 and puts the echoed string into it.
Syntax: >
Common options: n/a
cat
cat — Read a file, create a file, and concatenate files
cat is one of the more versatile commands and serves three main functions: displaying them, combining copies of them, and creating new ones.
Syntax: cat [option(s)] [file_name(s)] [-] [file_name(s)]
Common options: -n