Skip to content

Instantly share code, notes, and snippets.

@wesruv
Last active May 20, 2022 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesruv/f029d1755a2b909f1795075e3076b048 to your computer and use it in GitHub Desktop.
Save wesruv/f029d1755a2b909f1795075e3076b048 to your computer and use it in GitHub Desktop.
Linux CLI Beginner Cheatsheet

Basics

Here's some Linux (particularly Ubuntu) Command Line Interface (CLI) basics

# Command for listing files
ls

# Shows all files and in a nicer layout with the options l and a
ls -la

# The options 'l' and 'a' can be used separately as well, like this:
ls -l
ls -a

# In Ubuntu, you can simply type ll, which is aliased to ls -la
ll

# ls also takes an 'argument' for what folder it should be listing,
# by default it's the current folder, but you can get any folder 
# you have access to's content like this:
ls -la /var/www

# When exploring paths, ~ is a shortcut to your home folder,
# so this lists the contents of your home folder:
ls -la ~
# OR
ll ~

# This lists the contents of a folder called "dev" in your home folder
# In my case that's at /home/wesruv/dev:
ls -la ~/dev
# OR
ll ~/dev

# This tells you where you are currently:
pwd
# if I'm in my home directory on my machine, it would output: /home/wesruv 

# Traverse folders on your machine with cd

# Go to your home folder
cd ~

# Go to a folder called my-files, that's in your home folder
cd ~/my-files

# Off of the root of the hard drive, go to a folder called var, then to one called www
cd /var/www

Universal tips

Get help/documentation for a command

The --help flag is near universal flag on any command/program.

ls --help
cd --help

Do it as an Administratrator

sudo, meaning "Super user do", this means you're running a command as the root user, which gives the command god-like access to your machine. In other words, don't do this with any old code you find on the internet.

Sometimes it may be necessary to do things as root, for instance listing the contents of a directory only root has access to. It SHOULD NOT be necessary to run scripts from programs (unless you really trust them), or do tasks that aren't specifically getting into your operating system's innards.

It is necessary to install things with apt, update your OS, and other tasks that Windows or OSX would alert you for a password.

sudo can prefix any command, e.g.

sudo ls /some-really-locked-down-folder
sudo apt install fish

# Modify your hosts file to 'host hack' your machine (do at your own risk)
sudo nano /etc/hosts

Shutting down the machine:

# Shutdown:
sudo shutdown -h now

# Restart:
sudo shutdown -r now

Managing text files

I recommend nano for beginners, it's very common and most like text editors non-CLI folks are used to.

nano /PATH/TO/FILE.txt

Once you're in the editor you can use your keyboard to navigate and type (but not your mouse). The file name is at the top, the available commands are listed at the bottom. The ^ symbol means Ctrl key.

To exit, Press Ctrl + X It may then ask you if you want to save, Y or N for yes or no If yes it will ask you what file name you want to save it as, press enter to save over the file, change the name to make a copy.

Managing Files

Move or rename files/folders with mv, for example:

# Moves 'textfile.txt' (from the directory you're in) to your home directory, with the same name
mv textfile.txt ~/

# Moves 'textfile.txt' (from the directory you're in) to your home directory, and call it moved-textfile.txt
mv textfile.txt ~/moved-textfile.txt

# Change the name of a folder from `foo` to `bar`
mv foo bar

# Change the name of a text file from `hax.txt` to `system.ini`
mv hax.txt system.ini

Copy files

Works very similarly to mv, BUT copying files will change the owner and group of files to the user that copies them.

# Copy textfile.txt (in the directory you're in) to your home directory, with the same name
cp textfile.txt ~/

# Copy textfile.txt (in the directory you're in) to your home directory, and call it textfile-copy.txt
cp textfile.txt ~/textfile-copy.txt

# Copy a folder to your home directory
cp -r my-folders-name ~/

# Copy something with the same attributes (e.g. permissions, owner, group, timestamps)
cp -p textfile.txt ~/documents/
cp -rp my-folders-name ~/inside-this-folder/change-its-name-to-this

Deleting files and folders

There is no 'trash' or 'recycle bin' in Linux, if you delete it, it's gone. If you're not sure you want to delete something, you could try moving it to your home directory instead as a backup, then deleting the backup when you're sure.

# Removing a file called textfile.txt (that's in the same directory)
rm textfile.txt

# Delete a directory
rm -rf my-folder

Create a symlink (aka symbolic link)

Symlinks are like a portal from one part of your hard drive, to another. In ls they'll show up as their file name, and where they're linked to, e.g. www -> /var/www/

# Create a symlink from `www` in your home directory to `/var/www`
ln -s /var/www ~/www

# Create a symlink from `www` in your home directory to `/var/www`
ln -s /var/www ~/www

Permissions

When you run into permissions issues it can get frustrating fast, which can lead to doing rash things that will make things worse. When you run into a permissions error:

First, check what the permissions are, e.g. if we're having problems with Apache being able to read or write to files we might say:

ll /var/www

On my server that returns:

total 20
drwxrwxr-x  5 www-data   www-data   4096 Apr 29  2018 ./
drwxr-xr-x 14 root       root       4096 Apr 29  2018 ../
drwxrwxr-x  4 www-data   www-data   4096 Apr 29  2018 edu.wesruv.com/
drwxr-xr-x  2 www-data   www-data   4096 Apr 29  2018 html/
drwxrwxr-x  6 wruvalcaba wruvalcaba 4096 Feb 25  2020 wesruv.com/

The first entry is for ./, which means the current directory, the second entry ../ is for the parent directory; everything else is the contents of the current directory, which are all folders.

Let's break down a line:

drwxrwxr-x  4 www-data   www-data   4096 Apr 29  2018 edu.wesruv.com/

drwxrwxr-x The first part tells us the permissions in a very concise way.

  • The first letter d tells us this is a directory, it could also be a - for files, or l for a symlink.
  • From there, permissions consist of 3 groups of 3 items
  • The first 3 characters describe the permissions for the owner of the file
  • The 4-6 letters describe the permissions for the users in the 'group'
  • The last 3 characters describe the permissions for everyone else
  • Each three characters will be rwx (in that order) or a - if that user/group/others doesn't have that permission.
  • r is read
  • w is write
  • x is execute (open a directory, or run a file as an executable/program/script)

So drwxrwxr-x means:

  • It's a directory
  • Owner has read, write and execute permissions
  • Users in the group have read, write and execute
  • Others have read, and execute, but cannot write to the folder.

This permission could also be displayed as 775, this is another way of summarizing permissions in a binary-like form. Each number describes rwx:

  • In binary 7 = 111
  • For permissions, this means r = 1, w = 1, x = 1
  • 775 means the owner has all 3 perms, and group has all 3
  • In binary 5 = 101
  • For permissions, this means others have r and x, but not w out of rwx

Another common permission, usually on files, is 664:

  • In binary the 3 permissions groups would be: 110, 110, 100
  • So owner and group have rw but not x
  • Others have r but not wx

Next in the line you see www-data www-data this is the owner and group. On my system Apache has a user and group, both named www-data. So when you read permissions, the first two groups correspond to the user www-data, and users that are part of the group www-data, which I added my user to so I can edit files easily.

There can only be one owner to a file, so managing your groups so applications (like Apache) and users (like yourself or fellow devs) is something everyone has to deal with.

Next in that line we have the filesize 4096 in bytes

Next in that line is the timestamp for the creation of the file Apr 29 2018

Lastly, the name of the file: edu.wesruv.com/

Managing permissions

Changing permissions

# Change the permissions of my file to 664, aka rw-rw-r--
chmod 664 my-file.txt

# Change the permissions of my folder to 775, aka rwxrwxr-x
chmod 775 my-folder

Changing owner and/or group

# Change it so I'm the owner of my-text-file.txt
chown wesruv my-text-file.txt

# Change it so www-data (apache) is the owner, and my user's group is applied my my-folder
chown www-data:wesruv my-folder

# Change it so www-data is the owner, with my user's group for a folder, and every file and folder inside of /var/www
chown -r www-data:wesruv /var/www
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment