10+ Linux Commands Every Newbie Should Know
Tags: linux, codenewbie, programming, beginners
Published: 2021-08-21 00:11:46

Photo by <a href="https://unsplash.com/@pakata?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Pakata Goh</a> on <a href="https://unsplash.com/s/photos/coding?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
 
I mentioned in my [last blog post](https://dev.to/deteekay/how-i-plan-to-learn-to-code-before-2022-4o9p) that I will be [learning in public](https://www.swyx.io/learn-in-public/) and sharing what I learn online in order to hold myself accountable and measure my progress.

Well last month, I learned how to use the Linux command line. In [that post](https://dev.to/deteekay/how-i-plan-to-learn-to-code-before-2022-4o9p), I explained that I am currently curating my coding studies with [The Odin Project](https://www.theodinproject.com/) (TOP) curriculum. TOP encourages students to use a Linux system to learn coding, while those of us with a windows computer are taught how to access Ubuntu through a [virtual machine](https://www.virtualbox.org/). After installing the virtual machine, we then explored how to manipulate the command line.

My usual practice whenever I'm studying or learning something new is to jot down useful and relevant notes in a notebook. However, for the purpose of this learning-in-public journey, I have chosen to share my notes below 👇 

## Linux File System Operations

**Manipulating files and directories**

- to create a new directory type 👉 `mkdir <directoryname>`. To create intermediate or nested directories, the `mkdir` command will allow you to do so using the `-p` flag like this 👉 `mkdir -p Documents/book/page`.

- to create a file, use 👉 `touch <filename(s)>`. For example, to create a single file, type 👉 `touch novel.txt` and `touch letter.doc index.html script.js style.css` to create multiple files at once.

- to copy a file, use the `cp` command citing two arguments; the first argument is the file you want to copy (the source file) while the second argument (or target) is the location you want to copy the source file to. For instance, type 👉 `cp letter.doc book.doc`. When copying one directory to another, use the `-r` flag and specify both the source and target directories' paths. To copy only the *contents* of a directory, remember to use the star `*` operator at the end of the source directory path e.g `cp ~/book/* ~/Documents`.

- to delete directories, use `rmdir <directory name>` or `rm -d <directory name>` and `rm -r <directory name>` or `rm -rf <directory name>` for empty and non-empty directories respectively. To delete a file, type 👉 `rm <filename>` e.g, `rm script.js`. To confirm that a command is about to be executed use the `-i` flag, for instance to confirm that a file is about to be overwritten, type 👉 `cp -i <originfile> <targetfile>`. 

- to rename or move a file (or directory), use the `mv` command and type 👉 `mv <oldname> <newname>` for instance `mv book.doc index.html` to rename a file or `mv <directoryname> <targetdirectory>` to move a directory. You can also move a file or directory by first using the `cp` and then the`rm` commands. Remember to use the `-r` flag when copying directories. To move or copy a file to the current directory, specify the path/location of the file followed by `.` (which means the 'current directory') for instance type 👉 `cp ~/book/page.txt .`

- to print the results of a command to the console, add the `-v` flag. e.g, `rm -v book.doc`. Using this flag can provide useful reporting when writing scripts that will execute a lot of commands for you.

Note: most command line programs support *tab completion*, which involves automatically completing a word if there's only one valid match on the system. For example, if the only file starting with the letters 'Pic' is Picture, you could create the command to remove it as follows 👉  `rm Pic⇥` where ⇥ is the tab key. The program would then complete the filename, yielding `rm Picture`. This feature is especially handy  with longer filenames (or directories) where tab completion can save a huge amount of typing.

**Navigating Directories and Files**

- to find out where you are on the terminal, use the `pwd` command. You can also use this to get the path of your home directory. To see if a given program is available at
the command line, type 👉 `which <program name>`. The `which` command technically locates a file on the user’s [usr] path; which is a list of directories where executable programs are located.

- to see what files or directories are in your current directories, type `ls`. To see both 'visible' and 'hidden' files and directories, use the `-a` flag as follows 👉 `ls -a`. To find out the contents of another directory, without first leaving your current directory, specify the full path of the other directory to the `ls` command as follows 👉 `ls /home/username/Documents/novel`. To list directories' names only without listing their contents use the `-d` flag, for instance type 👉 `ls -d txt*` to list all files and directories starting with *txt* without listing their contents.

- to list all files and directories of a certain type for instance; for all files ending in *txt* pattern, type 👉 `ls *txt`; for all files starting with *txt*, type 👉 `ls txt*`; for all files containing *txt*, type 👉 `ls *txt*`. To find files whose names match a certain pattern or file extension e.g `.txt` starting in the current directory `.` and in its sub-directories, use the `find` command and type 👉 `find . -name '*.txt'`. 

- to find out detailed information on files and directories, type 👉 `ls -l`. To see the sizes of files and directories, in human readable format such as 1K or 23M, use 👉 `ls -lh`.  To list the long form of each file or directory in order of how recently it was modified (*reversed* so that the most recently modified entries appear at the bottom of the screen for easy inspection), type 👉 `ls -lrt`. To display the long form of files sorted so the largest files appear at the bottom, type 👉 `ls -lrS`.

- to navigate to the home directory, simply type 👉 `cd`. To navigate back up a directory (i.e to move one level up to the parent directory of the current directory) type 👉 `cd ..` To move change directories to the *previous* directory, wherever that was, type 👉 `cd -`. To move to an immediate sub-directory, type 👉 `cd <directory>` e.g `cd Documents`. To simply change directories, call the `cd` command by specifying to it the full path of the directory you want to navigate to e.g, `cd ~/book`.

- to redirect the output of one command to the input of another command, use the pipe operator `|`. For instance, to interactively navigate the full output of a command like `git help` in your terminal window, pipe the output to `less` as follows 👉 `git help | less`.

- to combine commands, use the semicolon `;` or double-ampersand `&&` characters after each command you want to combine e.g. to create a file in another directory without leaving your current directory, type 👉`cd <directory> && touch <filename> && cd -`. The difference between these two characters is that commands separated by `&&` execute only if the previous command succeeded while with `;` all the commands will be executed no matter what.

*Hey, thanks for reading!* 👋 👋

*This article was originally published on* [my blog](https://theconsistentdeveloper.hashnode.dev/)