Beginner Linux Commands

Linux has a huge number of command line commands, here I will feature some of the most useful in my opinion.

To open a terminal window from the Desktop enter: Ctrl+Alt+t

terminal window

Each command has options to modify the behavior. To learn about the function of a command and it’s options, the easy way is to type man command to view the command’s manual documentation. Press q to exit.

We tend to type: command options parameters

For operations affecting files that we do not own as a user, we may prefix our command with sudo if we have super user privileges.

Options are prefixed with a minus sign and multiple options may be combined after the minus sign (-xyz).

File system commands

List directory contents

ls

Simple example: ls -la

-a include hidden files

-l provide detailed listing

-h output human-readable file sizes

-t sort by time

-r reverse sort order

-R list subdirectories recursively

-d list directories

If your colors are set up for your terminal, folder and file names should be displayed in different colors when listed. In Linux, file names do not have to have extensions.

You can specify a pattern for what to list with * meaning any character(s) e.g. ls *.txt

pwd

Change directory

cd path

Path prefixes:

Home directory: ~/

Root of OS: /

Parent: ../

Current ./

Make a new empty file

touch filename

Although, this command has other uses.

Create a new directory

mkdir foldername

-p make parent directories as needed for a deep path to a new directory.

Remove a file

rm filename

-r remove directories and their contents recursively (drill down the tree)

-f force - ignore issues such as non-empty directories

-d remove empty directories

Copy files and directories

cp source destination

-a preserve settings such as read/write flags and owner.

-r copy recursively

-s create symbolic links instead of copying (the links point to the files but look like files in listings). Symbolic links might be used to enable features such as activated websites.

The destination of a file does not need to repeat it’s name, just the folder.

Move (rename) files

mv source destination

-n (no clobber) do not overwrite an existing file

-u update with new or newer file

This command is also used for renaming files e.g. mv oldname newname

RSYNC

This is a powerful command that is useful for backing up files recursively, only copying changed files, copying over a network, or using a secure link with an ssh identity file.

Copy files to a server

Copy in archive mode (files that are new or changed), with verbose output, with file compression over the network, and delete files not in the source folder

rsync -avz --delete temp/* user@domain:/home/user/temp

user@domain would have been previously added to say the ~/.ssh/config file so that a password or identity file is already known.

Make a local backup

rsync -av files /mnt/backups/

Secure copy

This is useful for transferring a file securely over a network.

scp -i ~/.ssh/identity-file source destination

Locations on a server are specified with user@domain:/path-on-server where domain may be the name in an ssh config file or the ip address of the server where the user has an account.

Report file system disk space usage

df

lsblk

Download a file from the www

wget url

Download online videos

Install youtube-dl

youtube-dl -r 330K url Limit the download rate to 330kB/s

Display file contents

cat filename

To display varying amounts of larger files; check out less and more commands.

File compression

For individual files we use gzip and for bundles we use tar to make an archive file called a tarball.

Compress single file

gzip filename This produces filename.gz

-k keep original file

-d decompress file

Compress multiple files

tar -czvf filename.tar.gz path-to-files

To extract the files:

tar -xvf filename.tar.gz

And to change the location of where to extract the files:

tar -xvf filename.tar.gz -C \home\user\temp for example.

User and group commands

Show your user name and group memberships

whoami

groups

Create a user

See man useradd

Modify a user account

See man usermod

List groups on system

We need to print the contents of a file using the cat command:

cat /etc/group

Create a new group

groupadd name

Change owner and group for a file or directory

sudo chown owner:group file

-R operate on files and directories recursively.

Change file permissions

sudo chmod mode file

This sets the permissions (read, write, execute) for the user (owner), group, or others. It is good practice to limit access based on permissions to only those that are in need of access to files.

The mode value is a set of 3 3-bit values read,write,execute for each type of access entity which may be expressed as octal numbers or abbreviations.

The values may be applied recursively, added individually, or removed etc.

Please see man chmod for the full details.

System commands

Show system time and date

date

Reboot a server

Sometimes, when you login to a server, you are advised that a reboot is required.

sudo reboot

Update package list

This is used on Ubuntu servers.

sudo apt update

And to upgrade the packages to the latest versions:

sudo apt upgrade

Close terminal

exit

List running processes

Get a snapshot:

ps

See realtime display of the processes:

top

Filter process list:

ps | grep -v grep | grep searchstring

The bar is used to “pipe” commands together.

grep is a filtering command. We filtered out the line containing the grep process in the above command.

To redirect command output to a file, we may use the > or » symbols to append the output.

ps > processfile

To stop a process from running, we take note of it’s ID from the listing of running processes and execute a kill processID command.

File editing

Open file for editing in nano (may need sudo if outside of user home directory):

nano file

Save file: Ctrl-o

Exit nano: Ctrl-x

Erase line: Ctrl-k

Finally

For copy and paste, I have to use the right-click mouse context menu (Ctrl-c, Ctrl-v doesn’t seem to work in the linux terminal).

So hopefully that gets you started with the basics of the Linux command line, and be sure to bookmark this site and explore the other ever-expanding range of topics to be found here.