Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Tips: Common CLI commands for beginners

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Tips: Common CLI commands for beginners

#1 Post by Hallvor »

Why bother with the command line interface (CLI)?

Using the CLI makes it easier to follow and give advice. While I am not advocating feeding your system with commands you do not understand, it is easier to copy and paste a command than clicking around in a GUI that is changing from version to another.

Although new commands are introduced from time to time, the original commands almost always remain the same.



Arch

The arch command is used to print the machine's architecture. For example:

Code: Select all

$ arch
x86_64

Cal/ncal

The cal and ncal commands will display a calendar:

Code: Select all

$ cal
   Februar 2019       
su må ty on to fr la   
               1  2   
3  4  5  6  7  8  9   
10 11 12 13 14 15 16   
17 18 19 20 21 22 23   
24 25 26 27 28        
Cat
The cat command prints the information of a file.

Code: Select all

$ cat debian.txt
Debian is awesome
cd
The cd command - change directory – allows you to navigate to a given directory.

Code: Select all

$ cd /home/hallvor/Music
chgrp
The chgrp command changes the group ownership of a file. Use group name as first argument and the name of the file as second argument.

Code: Select all

$ chgrp  debian debian.txt
chmod
The chmod command changes access permissions for a file, for instance making it executable:

Code: Select all

$ chmod +x debian.bin
Chown
The chown command changes ownership and group of a file. This example changes ownership and group to root:

Code: Select all

# chown root:root debian.txt
Clear
The clear command clears the terminal screen.

Code: Select all

$ clear
Cp
The cp command copies files and directories. This command will copy debian.txt to my documents folder in my home directory:

Code: Select all

$ cp debian.txt /home/hallvor/Documents
Date
The date command can print or set the system date and time.

Code: Select all

$ date 
må. 18. feb. 18:58:56 +0100 2019

Df
The df command displays the file system disk space usage in output.

Code: Select all

$ df 
Filsystem                             1K-blokker    Brukt Tilgjengelig Bruk% Montert på 
udev                                     3918824        0      3918824    0% /dev 
tmpfs                                     786256    17624       768632    3% /run 
/dev/mapper/debian--thinkpad--vg-root  236889936 44816920    179969952   20% / 
tmpfs                                    3931260    33948      3897312    1% /dev/shm 
tmpfs                                       5120        4         5116    1% /run/lock 
tmpfs                                    3931260        0      3931260    0% /sys/fs/cgroup 
/dev/sda1                                 240972    38829       189702   17% /boot 
tmpfs                                     786252        0       786252    0% /run/user/113 
tmpfs                                     786252       28       786224    1% /run/user/1000

Dir
The dir command lists directory contents.

Code: Select all

$ dir
Movies Documents debian.txt
Dpkg
The dpkg tool is a CLI package manager for Debian. To install a downloaded deb-file:

Code: Select all

# dpkg -i filename.deb
Exit
The exit command will exit the shell

Code: Select all

$ exit

Find

The find command will search for files in a directory and in its sub-directories.

Code: Select all

$ find debian*
debian
debian2
debian.txt
Free
The free command displays the amount of free and used memory in the system.

Code: Select all

$ free 
             total        used        free      shared  buff/cache   available 
Mem:        7862524     1513612      708188      258528     5640724     5816176 
Swap:       8069116           0     8069116
Grep
The grep command searches for a specified content in a file (or files) and displays the content:

Code: Select all

$ grep "Debian" debian.txt
Debian rocks!
Groups
Groups displays the name of groups a user is part of.

Code: Select all

$ groups hallvor 
hallvor : hallvor cdrom floppy audio dip video plugdev netdev bluetooth lpadmin

History
The history command displays the history of your shell commands.

Code: Select all

$ history
Kill
The kill command kills a process by sending the TERM signal to it.

Code: Select all

# kill [process-id]

Killall

The killall command kills a process by name.

Code: Select all

# killall konsole
Locate
The locate command helps user find a file by name. In Debian you may have to do the following to make the command work:

Code: Select all

# apt install locate && updatedb
Then, for instance:

Code: Select all

$ locate *.txt
/home/hallvor/debian.txt
/home/hallvor/test/debian.txt
ls
The ls command shows all non-hidden files, folders and directories.

Code: Select all

$ ls
Lscpu
The lscpu command displays the CPU architecture information:

Code: Select all

$ lscpu 
Arkitektur:            x86_64 
Op-tilstande for CPU:  32-bit, 64-bit 
Byterækkefølge:      Little Endian 
CPU'er:                4 
Tilkoblet cpu(er) liste:0-3 
Tråde per kerne:      2 
Kerner per sokkel:     2 
Sokler:                1 
NUMA-knuder:           1 
Leverandør-id:        GenuineIntel 
CPU-familie:           6 
Model:                 69 
Modelnavn:             Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz 
Modelserie:            1 
CPU MHz:               1204.980 
CPU maks. MHz:         3300,0000 
CPU min. MHz:          800,0000 
BogoMIPS:              5387.59 
Virtualisation:        VT-x 
L1d mellemlager:       32K 
L1i mellemlager:       32K 
L2 mellemlager:        256K 
L3 mellemlager:        4096K 
NUMA-knuder0 CPU'er:   0-3 
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dte
s64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb invpcid_single ssbd ibrs ibpb stibp kaiser tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 
avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts flush_l1d

Lspci
Prints detailed information about all PCI buses and devices in the system.

Code: Select all

$ lspci

Lsusb

Displays information about USB buses in the system and the devices connected to them.

Code: Select all

$ lsusb
Man
man lets you access the manual pages. For instance:

Code: Select all

$ man grep
Mkdir
The mkdir command lets you create directories.

Code: Select all

$ mkdir test
mv
The mv command (move) allows you to move a file or folder from the old location to the new location. (It can also be used to rename a file):

Code: Select all

$ mv /home/hallvor/debian.txt /home/hallvor/Dokument/debian.txt
Passwd
The passwd command changes passwords

Code: Select all

$ passwd hallvor
... for a user password, or

Code: Select all

# passwd root 
... to change the root password

Ping
The ping command is used to check if a system is up and responding.

Code: Select all

$ ping -c3 google.com 
PING google.com (216.58.211.14) 56(84) bytes of data. 
64 bytes from arn09s20-in-f14.1e100.net (216.58.211.14): icmp_seq=1 ttl=53 time=33.5 ms 
64 bytes from arn09s20-in-f14.1e100.net (216.58.211.14): icmp_seq=2 ttl=53 time=34.1 ms 
64 bytes from arn09s20-in-f14.1e100.net (216.58.211.14): icmp_seq=3 ttl=53 time=36.0 ms

rmdir

The rmdir (remove directory) command allows the user to remove an existing directory. For instance:

Code: Select all

$ rmdir test
... to remove an empty directory, or

Code: Select all

$ rmdir --ignore-fail-on-non-empty test
... to remove a directory and its contents


rm
The rm command (remove) will remove/delete files or directories with files in it. For instance:

Code: Select all

$ rm debian.txt
... or

Code: Select all

$ rm -r /home/hallvor/dokument
... to delete the documents folder and all the files and folders within it.

Su
The su (switch user) command lets you change user identity. The most common usage is to become root:

Code: Select all

$ su -
... and you can also use it to switch to another user

Code: Select all

# su - hallvor
Top
The top command lists running processes

Code: Select all

$ top
touch
The touch command makes an empty file

Code: Select all

$ touch debian.txt
Last edited by Hallvor on 2019-02-21 22:51, edited 8 times in total.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

Re: Tips: Common CLI commands for beginners

#2 Post by Head_on_a_Stick »

Nice guide Hallvor, thanks.

One thing though:
Hallvor wrote:The su (switch user) command lets you change user identity. The most common usage is to become root:

Code: Select all

$ su
It is recommended to use this instead:

Code: Select all

su -
If plain `su` is used then the normal user's environment is maintained so the sbin directories will be missing from PATH (along with a few other differences), only `su -` attains a full root environment.

And just to note that another nice guide to basic command line usage can be read with

Code: Select all

man intro
deadbang

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Tips: Common CLI commands for beginners

#3 Post by Hallvor »

Thanks, Head_on_a_Stick!

I have updated it.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

Bulkley
Posts: 6383
Joined: 2006-02-11 18:35
Has thanked: 2 times
Been thanked: 39 times

Re: Tips: Common CLI commands for beginners

#4 Post by Bulkley »

Agreed, that's a good guide, Hallvor. While I know and use most of them there are a couple new to me. Thanks for the refresher.

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Tips: Common CLI commands for beginners

#5 Post by Hallvor »

Bulkley wrote:Agreed, that's a good guide, Hallvor. While I know and use most of them there are a couple new to me. Thanks for the refresher.
You are welcome. :)
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
None1975
df -h | participant
df -h | participant
Posts: 1388
Joined: 2015-11-29 18:23
Location: Russia, Kaliningrad
Has thanked: 45 times
Been thanked: 65 times

Re: Tips: Common CLI commands for beginners

#6 Post by None1975 »

Very good guide. Thanks for sharing it!
OS: Debian 12.4 Bookworm / DE: Enlightenment
Debian Wiki | DontBreakDebian, My config files on github

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Tips: Common CLI commands for beginners

#7 Post by Hallvor »

None1975 wrote:Very good guide. Thanks for sharing it!
Thank you! :)
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Tips: Common CLI commands for beginners

#8 Post by sunrat »

Nice easy-to-follow guide and covers many of the most used commands.

Please check the dpkg entry. dpkg -i will install a package, not extract.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Tips: Common CLI commands for beginners

#9 Post by Hallvor »

Thank you. I have edited it.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
sickpig
Posts: 589
Joined: 2019-01-23 10:34

Re: Tips: Common CLI commands for beginners

#10 Post by sickpig »

Bookmarked
thanks for creating it

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2029
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 139 times
Been thanked: 206 times

Re: Tips: Common CLI commands for beginners

#11 Post by Hallvor »

sickpig wrote:Bookmarked
thanks for creating it
You are welcome.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
dotlj
Posts: 646
Joined: 2009-12-25 17:21

Re: Tips: Common CLI commands for beginners

#12 Post by dotlj »

Very nice, thanks for this.

How about

mail
apt update
apt install
apt-cache search (or aptitude search which includes more information, such as if installed or not)
grep pattern [file...]
wget or curl URL

and so on.

bullseye
Posts: 2
Joined: 2022-12-04 23:46

Re: Tips: Common CLI commands for beginners

#13 Post by bullseye »

Nice list. Maybe add

Code: Select all

apt-file
and

Code: Select all

who
to the list?
I'd be lost without

Code: Select all

info info

Code: Select all

help

Code: Select all

man -k $thing_you_want_to_know_about
OR

Code: Select all

apropos

User avatar
oswaldkelso
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1490
Joined: 2005-07-26 23:20
Location: UK
Has thanked: 1 time
Been thanked: 58 times

Re: Tips: Common CLI commands for beginners

#14 Post by oswaldkelso »

Hallvor wrote: 2019-02-18 18:23
Clear
The clear command clears the terminal screen.

Code: Select all

$ clear
This one always gets me when watching YouTube. What's wrong with CTL+l (small L)
Free Software Matters
Ash init durbatulûk, ash init gimbatul,
Ash init thrakatulûk agh burzum-ishi krimpatul.
My oldest used PC: 1999 imac 333Mhz 256MB PPC abandoned by Debian

User avatar
sunrat
Administrator
Administrator
Posts: 6412
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 116 times
Been thanked: 461 times

Re: Tips: Common CLI commands for beginners

#15 Post by sunrat »

+-+
oswaldkelso wrote: 2022-12-05 09:02
Hallvor wrote: 2019-02-18 18:23
Clear
The clear command clears the terminal screen.

Code: Select all

$ clear
This one always gets me when watching YouTube. What's wrong with CTL+l (small L)
I didn't know that one, then I don't know lots of keyboard shortcuts. Love it! Will definitely use in future.
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!

itmicp
Posts: 38
Joined: 2013-05-11 04:45
Location: France
Has thanked: 6 times
Been thanked: 6 times

Re: Tips: Common CLI commands for beginners

#16 Post by itmicp »

Hi

To close your shell without having to enter exit,
type Ctrl+D on an empty command-line.

Ctrl+D shortcut send an EOF,
see:

Code: Select all

stty -a
=======
To create an empty file, you can enter:

Code: Select all

> myEmptyFileName
=======
To create a text file without using any text-editor
you can enter this command line:

Code: Select all

cat > myFileName
then you can enter all you want to put in this file,
and/or type Ctrl+D to close the file.

=======
So, to create or add some lines at the end of a text file without using any text-editor,
you can enter this command-line:

Code: Select all

cat >> myFileName
then you enter all you want to add at the end of this file,
and/or type Ctrl+D to close the file.

User avatar
oswaldkelso
df -h | grep > 20TiB
df -h | grep > 20TiB
Posts: 1490
Joined: 2005-07-26 23:20
Location: UK
Has thanked: 1 time
Been thanked: 58 times

Re: Tips: Common CLI commands for beginners

#17 Post by oswaldkelso »

Here's some more bashshort cuts though I tend to use arrow keys or mc for moving and nano or mcedit for writing.

Ctrl+C Interrupt (kill) the current foreground process running in in the terminal
Ctrl+Z Suspend the current foreground process running in bash
Ctrl+L Clear the screen. This is similar to running the “clear” command
Ctrl+S Stop all output to the screen but not kill the process
Ctrl+Q Resume output to the screen after stopping it with Ctrl+S

Ctrl+A or Home: Go to the beginning of the line
Ctrl+E or End: Go to the end of the line

Alt+B Go (back) left one word
Ctrl+B Go (back) left one character

Alt+F Go (forward) right one word
Ctrl+F Go (forward) right one character

Ctrl+XX Toggle between the beginning of the line and the current position of the cursor

Ctrl+D Delete the character under the cursor
Alt+D Delete all characters after the cursor on the current line
Ctrl+H Delete the character before the cursor

Alt+T Swap the current word with the previous word
Ctrl+T Swap the last two characters before the cursor with each other

Ctrl+_ Undo your last key press. You can repeat this to undo multiple times

Ctrl+W Cut the word before the cursor to the clipboard
Ctrl+K Cut the line after the cursor to the clipboard
Ctrl+U Cut the line before the cursor to the clipboard
Ctrl+Y Paste from the clipboard. y stands for “yank”

Alt+U Capitalize every character from the cursor to the end of the current word
Alt+L Uncapitalize every character from the cursor to the end of the current word
Alt+C Capitalize the character under the cursor

Ctrl+P or Up Arrow: Go to the previous command in the command history
Ctrl+N or Down Arrow: Go to the next command in the command history
Alt+R Revert any changes to a command you’ve pulled from your history if you’ve edited it.
Ctrl+R Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command.
Ctrl+O Run a command you found with Ctrl+R.
Ctrl+G Leave history searching mode without running a command.

Tab Completion auto-completion e.g. $ my TAB will open myverylongfilename.txt if no other matches found.

--------
popd and pushd are useful if moving deep in the file system not that I use them I just start mc :-)
--------
Free Software Matters
Ash init durbatulûk, ash init gimbatul,
Ash init thrakatulûk agh burzum-ishi krimpatul.
My oldest used PC: 1999 imac 333Mhz 256MB PPC abandoned by Debian

Post Reply