Basic Bash Commands

Okan Özşahin
4 min readOct 7, 2023

We’ll explore a selection of frequently used Bash commands, along with explanations of their functionalities. These commands span a wide range of tasks, from navigating your file system to managing processes, scheduling tasks, and manipulating text. By gaining proficiency in these commands, you’ll be better equipped to navigate the command line, automate tasks, troubleshoot issues, and streamline your workflow in a Unix-like environment.

ls (List)

Description: Lists files and directories in the current directory.

Usage: ls [options] [directory]

Example:

  • ls lists files and directories in the current directory.
  • ls -l lists files and directories in a long format.
  • ls /path/to/directory lists files and directories in a specific directory.

cd (Change Directory)

Description: Changes the current working directory.

Usage: cd [directory]

Example:

  • cd /path/to/directory changes the current directory to the specified path.
  • cd .. moves up one directory level.
  • cd without an argument changes to the user's home directory.

pwd (Print Working Directory)

Description: Displays the current working directory.

Usage: pwd

Example:

  • pwd might return something like /home/user/documents

touch

Description: Creates an empty file with the given name.

Usage: touch filename

Example:

  • touch file.txt creates an empty file named file.txt

mkdir (Make Directory)

Description: Creates a new directory with the given name.

Usage: mkdir directoryname

Example:

  • mkdir myfolder creates a directory named myfolder

rmdir (Remove Directory)

Description: Removes an empty directory.

Usage: rmdir directoryname

Example:

  • rmdir myfolder deletes the directory myfolder if it's empty.

rm (Remove)

Description: Deletes files or directories.

Usage: rm [options] filename/directory

Example:

  • rm file.txt deletes the file file.txt.
  • rm -r myfolder recursively deletes the directory myfolder and its contents.

cp (Copy)

Description: Copies files or directories from one location to another.

Usage: cp [options] source destination

Example:

  • cp file.txt /path/to/destination copies file.txt to the specified destination.

mv (Move)

Description: Moves files or directories from one location to another or renames them.

Usage: mv [options] source destination

Example:

  • mv file.txt newfile.txt renames file.txt to newfile.txt.
  • mv oldfolder/ newfolder/ moves the oldfolder to newfolder.

cat (Concatenate)

Description: Displays the content of one or more files.

Usage: cat [options] filename(s)

Example:

  • cat file.txt displays the contents of file.txt.

grep (Global Regular Expression Print)

Description: Searches for patterns in text using regular expressions.

Usage: grep [options] pattern [file(s)]

Example:

  • grep "keyword" file.txt searches for the word "keyword" in file.txt.

find

Description: Searches for files and directories in a specified location.

Usage: find [path] [options] [expression]

Example:

  • find /path/to/search -name "file.txt" searches for files named "file.txt" in the specified path.

ps (Process Status)

Description: Lists running processes.

Usage: ps [options]

Example:

  • ps aux displays a detailed list of all running processes.

kill

Description: Terminates processes using their process IDs (PIDs).

Usage: kill [options] PID

Example:

  • kill 1234 sends a signal to terminate the process with PID 1234.

wget (Web Get)

Description: Downloads files from the internet.

Usage: wget [options] URL

Example:

  • wget https://example.com/file.zip downloads a file from the given URL.

chmod (Change Mode)

Description: Changes file permissions.

Usage: chmod [options] permissions file(s)

Example:

  • chmod +x script.sh adds execute permission to script.sh.

chown (Change Owner)

Description: Changes file ownership.

Usage: chown [options] user:group file(s)

Example:

  • chown user:group file.txt changes the owner and group of file.txt.

tar (Tape Archive)

Description: Compresses and archives files and directories.

Usage: tar [options] archive-file files/directories

Example:

  • tar -cvf archive.tar file1 file2 creates a tar archive with files file1 and file2.
  • tar -xvf archive.tar extracts files from the archive.tar file.

psgrep (Process Grepper)

Description: Filters and lists processes based on a specified keyword.

Usage: ps aux | grep keyword

Example:

  • ps aux | grep firefox lists all processes related to Firefox.

ssh (Secure Shell)

Description: Establishes secure shell connections to remote hosts.

Usage: ssh [options] user@hostname

Example:

  • ssh user@hostname connects to a remote server using SSH.

ping

Description: Sends ICMP echo requests to a host to check network connectivity.

Usage: ping [options] hostname/IP

Example:

  • ping google.com sends ping requests to Google's servers.

ifconfig (Interface Configuration)

Description: Displays network interface configuration.

Usage: ifconfig [interface] [options]

Example:

  • ifconfig eth0 displays information about the Ethernet interface eth0.
  • ifconfig en0 | grep inet displays information about own ip address.

netstat (Network Statistics)

Description: Shows network-related information, including open ports and connections.

Usage: netstat [options]

Example:

  • netstat -tuln displays a list of listening TCP and UDP ports.

cron

Description: Used to schedule tasks or jobs to run at specific times or intervals.

Usage: crontab [options]

Example:

  • crontab -e opens the user's crontab file for editing to schedule periodic tasks.

echo

Description: Prints text or variables to the terminal.

Usage: echo [options] [text]

Example:

  • echo "Hello, World!" prints "Hello, World!" to the terminal screen.

basename and dirname

Description: Extracts the filename or directory name from a given path.

Usage: basename [path] and dirname [path]

Example:

  • basename /path/to/file.txt returns file.txt
  • dirname /path/to/file.txt returns /path/to

curl (Client for URLs)

Description: A tool to transfer data to or from a server using various protocols.

Usage: curl [options] URL

Example:

  • curl https://example.com retrieves the content from the specified URL.

lsof (List Open Files)

Description: Lists open files and the processes that have them open.

Usage: lsof [options] [file/directory]

Example:

  • lsof -i :80 lists processes using port 80.

These are just some of the fundamental Bash commands. There are many more commands and options available for specific tasks and requirements. You can find more information and detailed explanations for each command by using the man command followed by the command name, like man ls for the manual page of the ls command.

--

--

Okan Özşahin

Backend Developer at hop | Civil Engineer | MS Computer Engineering