This is an old revision of the document!
Table of Contents
This article is Part 1 in a series of cheat-sheets on the command line shell, bash. (Previous Page | Next Page)
bash: Getting Started
Finding Commands and Docs
which, whereis & type
Use these commands to dig up more info on command commandname:
$ which -a commandname
$ whereis commandname
$ type commandname
$ type commandname -a
You can also run either -h
or –help
on most commands, for a list of available flags.
history
View a history of commands entered in the shell:
$ history
man
The man
utility is key to finding documentation within your Linux system. Most of these cheat sheets you are reading now, were simply distilled from man pages that are probably already on your machine.
Search for commands matching keyword:
$ man -k keyword
View man pages for command commandname:
$ man commandname
View man pages for command commandname in sec. 7:
$ man 7 commandname
Man Page Controls
E / Y
(Forward/Backward one line)D / U
(Forward/Backward half window)Space / B
(Forward/Backward one window)H
(Display help file)Q
(Exit)
Man Page Section Areas
- 1 (Executable programs or shell commands)
- 2 (System calls)
- 3 (Library calls)
- 4 (Special files)
- 5 (File formats and conventions)
- 6 (Games)
- 7 (Overviews, conventions, and misc)
- 8 (Super user and sysadmin commands)
- 9 (Kernel routines)
Shell Traversal
Open new bash shell as a child of current one:
$ bash
Exit current bash shell:
$ exit
Show shells by current user:
$ ps --forest
Show list of installed shells:
$ cat /etc/shell
Opens new shell for user username as child of current shell:
$ su username
You can use who am i
to see who you are logged in as. You can bring up more identity info with the commands id
and who -uh
.
Filesystem Traversal
Show present working directory:
$ pwd
Show file tree for path:
$ tree path/
Shows source target of link:
$ readlink -f /path/to/link
ls
$ ls
Display directory listing. Some options include:
-F
(With folders identified)-A
(With hidden dotfiles)-l
(Long listing format)-lh
(Human-readable long listing format)-t
(Sort by modification time, newest first)-C
(List by columns)-X
(Sort alphabetically, by extension)-R
(Recursive into subdirectories)-i
(Show inode number)
Viewing Contents of Files
Determine file type of file filename:
$ file filename
cat
$ cat filename
Display contents of file filename. Some options include:
-n
(Numbers output lines)-T
(Suppress tab characters)
more
$ more filename
Similar to cat
but pages results.
less
$ less filename
Similar to more
but with vi
controls.
tail
Displays last 10 lines of file filename:
$ tail filename
Displays last 3 lines:
$ tail -n 3 filename
head
Displays first 10 lines of file filename:
$ head filename
Displays first 3 lines:
$ head -n 3 filename
sort
$ sort filename
Displays sorted contents of file. Some options include:
-n
(For sorting numbers correctly)-M
(For sorting months correctly)–output=FILE
(Spits results out to file)
Example - Sort password file numerically based on column 3 (UID):
$ sort -t ':' -k 3 -n /etc/passwd
Searching For Files
locate
$ locate
Searches updatedb for files where any part of path matches string.
find
$ find
Searches filesystem live based on file attributes.
Searching Within Files
grep
$ grep searchterm filename
Search filename for searchterm and output lines in file where it is found. Some options include:
-n
(Show line numbers on results)-v
(Output lines that don't match)-c
(Output a count of result lines)
Searches path for searchterm:
$ grep searchterm path/
Basic File and Directory Commands
touch
Create an empty file named filename:
$ touch filename
mkdir
Create a new folder named newfolder:
$ mkdir newfolder
Create a new tree of folders at once:
$ mkdir -p newfolder/subfolder/subfolder
cp
$ cp sourcefilename destinationfilename
Copy file. Some options include:
-f
Force any overwrites-i
Force shell to ask if you want to overwrite-n
Do not overwrite targets-R
Recursive into subdirectories
Examples:
Copy file into destination path:
$ cp sourcefilename destinationpath/
Copy file into pwd:
$ cp /source/filename .
Copy folder:
$ cp -R sourcefolder/ destinationfolder
Copy folder into pwd:
$ cp -R /source/folder/ .
ln
Create hard link in pwd:
$ ln sourcefilename ./linkname
The flag -s
creates a symlink instead of hard link.
mv
To rename files, you intuitively use the move command:
$ mv sourcefilename destinationfilename
Move file to another path and rename:
$ mv /path/sourcefilename /anotherpath/destinationfilename
rm and rmdir
$ rm filename
Delete file. Some options include:
-f
(Force delete)-i
(Prompt before every removal)-d
(Remove empty directories)-r
(Remove directories and contents recursively)
Delete all files within the PWD:
$ rm ./*
Delete all files matching extension .bar within PWD:
$ rm ./*bar
Delete folder:
$ rmdir foldername
rmdir
will not delete a folder with contents. Use rm -rf foldername/
to have rm to recursively delete the folder and its contents instead.
Find this page online at: https://bestpoint.institute/tools/bash/getting-started