Bash Commands Guide

Bash is an interactive command line interpreter or shell. This is a list for time-saving purposes with common usage bash commands and what they are.

Basic Commands

LS

Lists the folder and file names in the current working directory.

1
2
3
4
5
6
7
8
ls -a  # Lists all files including hidden files 
ls -A  # Lists all files, including hidden files, except a top directory
ls -F  # Add an indicator (one of */=>@|) to entries
ls -S  # Sort by file size
ls -al # Provides a list of all the files in the same directory
ls -l  # Use a long listing format
ls -nl # Use a long listing format with user ID
ls -c  # List contents by columns

MAN

It is the interface used to view the system’s reference manuals.man ls also displays all the options available for running the command.

1
2
3
4
man -h # Print a help message and exit
man -V # Display version information and exit
man -C # Use the configuration file rather than the default of ~/.manpath.
man -d # Print debugging information.

ENV

Returns a list of the environment variables for the current user.

1
env

CHMOD

To change the permissions of files or directories.

1
2
3
chmod 777 # Anyone can read, write and execute chmod 777 my_file
chmod 755 # Files should be readable and executable by others, but only changeable by the issuing user
chmod 700 # Only the user can do anything to the file

CHOWN

Changes ownership of files and directories.

1
chown --help && chown --version

PATH

It stores a list of directories separated by a colon.

1
echo $PATH

GREP

Searches files for lines that match a pattern and returns the results.

1
2
3
grep "keyword" file    # Searches for the given string in a single file
grep -i "keyword" file # Enables the command to be case insensitive
grep -R "keyword" file # Searches all files in a directory and outputs filenames and lines containing matched results

AWK

Extracts just a list of specific things.

1
awk {keyword} file.tx

OPEN

Opens current folder in finder.

1
open .

CHGRP

Changes the group of the file and directory.

1
chgrp groupname file.txt

PWD

Command prints the name of working directory.

1
pwd

CD

Changes the current working directory.

1
2
3
4
cd /     # Goes to the root directory
cd ..    # Goes to a parent directory
cd       # Goes to the user's home directory when executed without the parameter
cd ~root # Goes to the user's home directory when the user name is given after the ‘~’

HOME

Displays the path of the home directory.

1
echo $HOME

NANO

Command line text editor and only accepts keyboard input.

1
nano myscript.sh

FIND

Searches for files and directories.

1
2
3
4
5
6
7
8
9
find directory -name filename # searches the file by name
find directory -cnewer file   # The file was last changed more recently than the file was modified
find directory -amin n        # The file was last accessed n minutes ago
find directory -cmin n        # The file was last changed n minutes ago
find directory -atime n       # The file was last accessed more n days ago
find directory -mtime n       # File’s data was last modified n days ago
find directory -ctime n       # The file was last changed more than n days ago
find directory -print         # Displays the path name of the files found by using the rest of the criteria
find directory -exec          # Executes commands on the resulting search results

MKDIR

Creates directories with this command.

1
mkdir folder: This command would create the sub folders

MV

Moves a file or directory as another file and directory.

1
2
3
4
mv -i source destination # Prompt before overwriting an existing file
mv -f source destination # Always overwrite existing files without prompting
mv -n source destination # Never overwrite any existing file
mv -v source destination # Print source and destination files

RM

Removes objects.

1
2
3
4
rm -f file      # Does not ask questions during deletion, does not provide information for a non-existent file
rm -i file      # Prompt before deleting files
rm -r directory # Deletes the contents of directories and subdirectories consecutively
rm -v file      # Gives more detailed information about the remove process

RMDIR

Removes directory, this command only works if the folders are empty.

1
rmdir -p directoryname

TOUCH

Creates or opens a file and saves it without any change to the file contents.

1
2
3
4
touch -a file # Updates file access time
touch -c file # If the file does not exist, it creates the file
touch -m file # Changes the file modification time
touch -t file # Changes the file access or replacement time as specified

CAT

Shows the contents of the files.

1
cat -n file # Prints the contents of the file on the screen by giving a number to all lines

WC

Prints newline, word, and byte counts for each FILE, and a total if more than one FILE is specified

1
2
3
wc -l file # Returns the number of rows
wc -m file # Returns the number of characters
wc -w file # Counts the number of words

Displays the first lines of lines of the file.

1
head file: Shows the first couple lines

TAIL

Displays the last couple of lines of the file.

1
tail file: Shows you the last couple lines

MORE

The contents of the file are used to display the page page.

1
more file

LESS

Allows backward movement in the file as well as forward movement.

1
less file

NL

Adds a line number per line.

1
nl file

Cheat Sheet

List number of files on a directory

1
2
3
4
5
6
ls -l . | egrep -c '^-'

# savefile list to txt
ls -p | grep -v / > filelist79.txt

#echo mv "$f" "${f// /_}"