Author: Amil Khan

So you want to start using the terminal more but too scared to type anything because you feel you might break your computer.

The Basics

   ‌

1. ls

How to list what's in a directory/folder.

ls

About 50% of the time, I am using ls -al where the -l flag prints using a long listing format and -a includes hidden files like .git and the notorious .DS_STORE. Here are some more common flags that you can use to view user/group permissions, sort files by modified time, view file sizes, and a ton of other useful information.

ls -al

ls -ltr

ls -larth

ls -1               

Below is a list of my favorite flags to use with the ls command. I purposefully did not add comments to the commands because I want you to figure out what each command does.

-a, --all do not ignore entries starting with . 
-A, --almost-all do not list implied . and .. 
-C     list entries by columns --color[=WHEN] colorize the output; WHEN can be 'always' (default if omitted), 'auto', or 'never'; more info below 
-d, --directory list directories themselves, not their contents 
-D, --dired generate output designed for Emacs' dired mode 
-f     do not sort, enable -aU, disable -ls --color 
-F, --classify append indicator (one of */=>@|) to entries --file-type likewise, except do not append '*' -g     like -l, but do not list owner 
-G, --no-group in a long listing, don't print group names 
-h, --human-readable with -l and/or -s, print human readable sizes (e.g., 1K 234M 2G) 
-i, --inode print the index number of each file 
-I, --ignore=PATTERN do not list implied entries matching shell PATTERN 
-l     use a long listing format 
-L, --dereference when showing file information for a symbolic link, show information for the file the link references rather than for the link itself 
-m     fill width with a comma separated list of entries 
-n, --numeric-uid-gid like -l, but list numeric user and group IDs 
-N, --literal print entry names without quoting 
-o     like -l, but do not list group information 
-p, --indicator-style=slash append / indicator to directories -r, --reverse reverse order while sorting 
-R, --recursive list subdirectories recursively -s, --size print the allocated size of each file, in blocks 
-S     sort by file size, largest first 
-t     sort by modification time, newest first 
-U     do not sort; list entries in directory order 
-v     natural sort of (version) numbers within text 
-w, --width=COLS set output width to COLS.  0 means no limit 
-x     list entries by lines instead of by columns 
-X     sort alphabetically by entry extension 
-1     list one file per line.  Avoid '\n' with -q or -b

For the full documentation of the ls command, run the following in your terminal:

man ls

2. cd

How to change directories/folders. Yes, directory and folder are the same.

Let's say I open a fresh terminal and my directory structure of /home/amil looks like this

amil 
├── bisque
├── Documents
└── surfing

So here I can change directories into one of these three folders and use ls to see what is in them.

❯ ls bisque   Documents   surfing 

❯ cd bisque 

❯ ls bqapi   bqengine   bqserver   bqcore

When you first open your terminal on a linux machine, it will most likely put you in /home/USER where USER is your username on the computer. For me its amil, so /home/amil .

Seems easy? This is how you move in the terminal. Similar to clicking on a folder and exploring the contents using the User Interface (UI), you can achieve the same result faster using bash.

# Move DOWN on directory 
❯ cd bisque 


# Move DOWN one more 
❯ cd bqapi 


# What's my current path
❯ pwd /home/amil/bisque/bqapi 


# Take me back to the top
❯ cd 
❯ pwd /home/amil 


# Shit, I accidently typed cd, take me back to where I was! 
❯ cd - 
❯ pwd /home/amil/bisque/bqapi 


# Okay, now I need to go UP one directory 
❯ cd .. 
❯ pwd /home/amil/bisque

3. mv

Now I want to move a file to a different location. Think of dragging and dropping a file from Downloads to Documents.

mv

Let's say I have a directory structure like this in /home/amil

❯ ls Downloads   Documents   surfing 

❯ ls Downloads cat.png    musk-tweets.txt    dataset.zip

Now I want to move dataset.zip from Downloads to Documents.

# move dataset.zip from Downloads to Documents 
mv  Downloads/dataset.zip  Documents 
|            |                 | 
MOVE       SOURCE         DESTINATION

Now dataset.zip is in Documents.

Renaming a File

There is no renaming command in bash. You would use the mv command to "rename" a file. Let's say I want to rename dataset.zip to something more meaningful. We would do this by moving the file with a different name.

mv dataset.zip imagenet-FullDataset.zip

4. df

This commands shows you how much space you have on your computer's hard drive(s). I usually use df with the human readable flag -h which shows the usage in MB/GB/TB instead of blocks. If you are hardcore and prefer blocks, just use df.

# Adding the flag -h since I am human 
df -h

5. du

Another incredibly useful command that shows file space usage. Let's say I want to see how large the Downloads folder is on my laptop. I can run du with the -s flag for summarizing and -h for human readable.

du -sh

6. cp

Of course there would be a copy command. Now I want to copy a file from one folder to another.

❯ ls 
Downloads   cat.png    chatgpt-shakespeare.txt    dataset.zip

# Copy cat.png to Downloads
cp cat.png Downloads

7. rm

Time to cleanup random files we don't need anymore. You can use rm to remove files and folders, but be careful, you are deleting stuff!

❯ ls 
Downloads   cat.png    nsfwMemes-tweets.txt    dataset.zip

# Remove/Delete nsfwMemes-tweets.txt
rm nsfwMemes-tweets.txt

If I want to really get rid of a file, I can use shred.

🌪️
Shred
Usage: shred [OPTION]... FILE...
Overwrite the specified FILE(s) repeatedly, in order to make it harder for even very expensive hardware probing to recover the data.

You will probably only use shred if you are selling your computer and need to wipe the hard drive(s). For deleting files/folders, use rm.

8. mkdir

So now you need to make a new folder to make your life a bit more organized. Well, there's a command for that, Make Directory, or mkdir

❯ mkdir homework

# Did it work?
❯ ls 
Downloads   cat.png    homework    dataset.zip

9. cat

Have you ever wanted to just view the contents of a file? Like nothing fancy, just see what a .txt file contains. Good thing there's cat which allows you to display the contents of pretty much any file in the terminal. If you try to cat a binary file, you will get a messy output though, so don't go crazy.

cat iris-dataset.csv

10. find

I need to find all files that end with .txt in the current directory and its subdirectories. Luckily, there's the find command!

find . -name "*.txt"

The . syntax here refers to the current directory and -name specifies the pattern to match. This is useful for finding those weird Mac .DS_Store files that magically appear in your git push.


⭐️ Bonus ⭐️

There are a couple of commands I use everyday, but you need to apt install them.

htop

This is my go-to process viewer and I pretty much always have this up on one of my terminals, especially when I am diagnosing memory issues with my PyTorch training runs.

It even runs inside a Docker container, which is what I am showing here. That's why you only see two processes.

To install htop, run the following command

sudo apt install htop

Then start using it!

htop

Vim

sudo apt install vim

No excuses, just use Vim. Nano sucks, use Vim. Nuff said.

Okay, jokes aside, both achieve the same results for editing files. The only difference is Vim requires you to put in time to learn Vim Commands. Once you learn maybe 15-20 Vim commands, you will work twice as fast, money back guarantee.

You can use Vim to open any file and immediately start editing the file. If you remember, cat allows you to view the content of files and vim allows you to edit the content of files.

Here I am visualizing a script that I opened with Vim from the great people at Helm Charts for Kubernetes.

vim get-helm.sh

I can use the arrow keys to move up and down, and I can make changes to the file by going into INSERT mode by typing i. Once I am done, I can hit the esc key and then type :wq which stands for WRITE and QUIT.

These are the very basics of Vim and I highly recommend you go through the vimtutor in the terminal.

vimtutor
Share this post