Bash Commands I

Bash Commands I #

This section introduces the basic bash commands that every user should be familiar with to efficiently work with the command line. These commands will help you navigate the filesystem, manipulate files and directories, and perform other fundamental operations. Familiarizing yourself with these commands is the first step in becoming proficient with the terminal.

pwd #

pwd     # Print the full filename of the current working directory

Type this if you’re lost! That output can be especially useful when used as an environment variable in scripts, which we’ll see later.

cd #

Use it to change directory.

cd foo/         # Go to the foo/ directory
cd foo/bar/     # Go to the bar/ subdirectory located in foo/
cd ..           # Go one level up, to parent directory
cd ../../foo/   # Go two levels up and to the foo/ subdirectory located there
cd /tmp/        # Go to the tmp/ directory at the root (/) of the filesystem
cd ~ or cd      # Go to your user home directory
cd -            # Go to your last visited directory

You can use paths relative to your home directory e.g. cd ~/../../tmp/ will bring you to /tmp/.

ls #

Use it to list directory content. ls comes with many useful flags.

ls      # List files and directories
ls -a	# ... including "hidden" ones starting with .
ls -l	# ... with long format, showing permissions on each
ls -F	# ... with indicators like * for executable files or / for directories
ls -t	# ... sorted by modification time
ls -h	# ... with human readable sizes

For most commands, compatible flags can be used and grouped together and optional paths can be defined if you’re not already in the relevant directory.

The single command from the example below lists the content of the parent directory, including hidden items, in long format with appended indicators with human readable sizes.

ls

Creating & Removing Files #

touch #

touch foo.txt # Create empty foo.txt or update foo.txt with modified timestamp

You can also create multiple files at once. The following three commands are equivalent:

touch foo1.md foo2.md foo3.md bara.txt barb.txt barc.txt
touch {foo1,foo2,foo3}.md {bara,barb,barc}.txt
touch foo{1..3}.md bar{a..c}.txt

You can create files with content by using redirections (more on that in the next section Bash Commands II). Use > operator to redirect the output of a command to a file, and the >> operator to append to it.

echo "1st Line" > foo.txt   # Create or overwrite foo.txt with echo output
echo "2nd Line" >> foo.txt  # Append new echo output to foo.txt
echo "3rd Line" >> foo.txt  # Append new echo output to foo.txt

This would be equivalent to :

echo -e "1st Line\n2nd Line\n3rd Line" > foo.txt

Linux is case-sensitive and command flags are too.

echo -e is very different from echo -E (default behavior) in that the first flag will interpret backslash escapes and the other will ignore them.

In this case, echo -eE makes no sense and would be unpredictable.

Redirections work with many commands.

ls -alF > myfiles.txt # Create or overwrite myfiles.txt with the `ls` output

rm #

rm foo.txt         # Remove foo.txt
rm -f *.txt .*.txt # Remove all .txt files from the directory, hidden ones too

The rm -f command can be useful in this case as it allows you to bypass any error that may occur if one of your specified patterns does not match any item.

Reading Files #

cat foo         # Print foo content
less foo        # Print navigable, searchable foo content
head foo        # Print top 10 lines of foo content
tail -15 foo    # Print bottom 15 lines of foo content
wc foo          # List number of lines, words and characters from foo content

The less command is a useful utility for viewing text files or command outputs in a terminal. It opens the file in a scrollable interface, allowing you to navigate and search through it with ease.

Here are some basic navigation commands in less:

  • Up and Down → Use the arrow keys or j (down) and k (up) to scroll through the file one line at a time.
  • Page Up and Page Down → Use the PgUp and PgDn keys or ^U and ^D to scroll through the file one page at a time.
  • Top and Bottom → Use g to go to the beginning of the file and G to go to the end.
  • Search → Press / followed by the search term and hit return. Use n to go to the next occurrence and N to go to the previous occurrence.
  • Quit → Press q to exit the less command and return to the command prompt.

Creating & Removing Directories #

mkdir #

Use it to make directories.

mkdir foo             # Create the foo/ directory
mkdir foo bar         # Create directories foo/ and bar/ at the same level
mkdir -p foo/bar/baz/ # Create nested directory baz/
The -p flag will take care of creating all non-existent directories leading to baz/

rmdir #

Use it to remove directories.

rmdir foo/      # Remove empty foo/ directory

rmdir is restricted to delete only empty directories. For convenience, you can use rm with the recursive flag instead.

rm -r foo/      # Remove foo/ directory and ALL of its content

Copying & Moving #

cp #

As in copy.

cp foo.txt bar.txt          # Copy foo.txt as bar.txt
cp foo.txt foos/            # Copy foo.txt into the foos/ directory
cp foo.txt bars/bar.txt     # Copy foo.txt as bar.txt into the bars/ directory
cp -r foo/ bar/             # Copy foo/ and its content as bar/

mv #

As in move.

mv foo.txt foos/            # Move foo.txt to foos/
mv foos/ /tmp/              # Move foos/ to /tmp/

You can use the same mv command to easily rename a file or directory.

mv foo.txt bar.txt          # Rename foo.txt as bar.txt
mv foos/ bars/              # Rename foos/ as bars/