DEV Community

50 basic Linux commands

Nnamdi Kenneth Nwosu on July 08, 2024

Here are 50 basic Linux commands that are useful for navigating and managing your system: pwd - Print working directory. ls - List directory co...
Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

The touch command is actually used to update the last modified time and last accessed time of a file or directory. The fact that it creates a new empty file if the given filename does not exist is a convenient side-effect, but not the main function of the command.

A far simpler (and shorter) way is to just use >myFileName.txt, which will create the file 'myFileName.txt'

Collapse
 
shricodev profile image
Shrijal Acharya

Exactly, it should be one of the most misunderstood CLI command. Almost everyone thinks of the "touch" command as a way to create an empty file.

I guess not everyone uses man pages to learn a new CLI tool. πŸ€·β€β™‚οΈ

Image description

Collapse
 
dev-nnamdi profile image
Nnamdi Kenneth Nwosu

Touch : to create a file without any content

Collapse
 
seanbracksdev profile image
SeanBracksDev • Edited

I wouldn’t say it’s misunderstood.
It’s a side-effect yes, but it’s still the simplest and easiest way to create an empty file, hence why it’s used this way.

Thread Thread
 
shricodev profile image
Shrijal Acharya

Exactly. It's just that the command is not primarily used for its main purpose but rather as a fallback feature. However, the fallback feature seems more of a robust use case rather than its main feature.

Collapse
 
josephj11 profile image
Joe

I expect that few people use man pages to learn new commands. Man pages remind you very tersely what a command does and tell you what the calling sequences and options are. Most do not even include one example. They exist primarily to quickly remind you of how to use things you already understand. The GNU info pages, when they exist, are usually far better.

Collapse
 
davidgfriedman profile image
David G Friedman

Use "touch" to backdate a file's timestamp. There are legitimate uses for that btw.

Collapse
 
stevenev38 profile image
Steven McCloud

Lol Certainly. Some recommended edits/additions

ifconfig is now outdated, as net-tools doesn't come by default anymore on most systems. ip is the successor.
Same goes for vi. Vim has the same/even more functionality and is the successor of vi (Vi-IMproved)β€”though it's true from my experience that vi typically comes out of the box, and vim does not

Some recommended additions:
dd
history
Redirections (>, >>)
usermod
adduser
addgroup
tail
head
less
more
lsmod
lsblk

Regarding touch, previous commenters are correct that it's primary function is to modify timestampsβ€”which is still important when building and compilingβ€”and no other basic tool has the functionality, so it's still significant for it's primary function. But on that note, one could argue that tools with which the primary purpose is to aid in the builds and compiling process would not fall under the "Basic Linux commands" category soo Β―_(ツ)_/Β―

On that note, commands I do think should fall under basic toolsβ€”in that they're important to know in order to experience the the primary selling point of Linux, i.e. the functionality and efficiencyβ€”are as follows:
awk
ln
tee
cut
tr
sort
uniq

Collapse
 
dev-nnamdi profile image
Nnamdi Kenneth Nwosu

Thank so much

Collapse
 
alex_mondale_dbb58b28000a profile image
Alex Mondale

du and df are daily drivers for me.

Collapse
 
0nth0 profile image
Navneet Thillaisthanam

If you have iproute2 installed, then you can use ss (Socker Statistics) to see the status of the ports. This is cleaner than netstat

Collapse
 
orenc profile image
Chanan Oren

ss is also the preferred tool since netstat is deprecated.

ifconfig and route are also deprecated, use ip address (can be shortened to ip addr or ip a) and ip route for those.

Also, dnf instead of yum on newer RPM-based distros.

Collapse
 
dpacuszka profile image
dpacuszka

sudo su is redundant, just use sudo -i

Collapse
 
tienttm profile image
Quang Tien Tran

Line 22 and 48 are the same

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Generative AI often does stuff like that πŸ˜‰

Collapse
 
shricodev profile image
Shrijal Acharya

Certainly! The first word provided another big hint that this might have been generated by Generative Models. It's fine to use those models as long as you're not simply copying and pasting, but rather gaining insights and using them thoughtfully. @dev-nnamdi, please don't take it the wrong way. 😊

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

If indeed this is the case, there are guidelines that should be followed for such content.

Collapse
 
dev-nnamdi profile image
Nnamdi Kenneth Nwosu • Edited

No, was a mistake, fixed it

Collapse
 
peter-fencer profile image
Peter Vivo

With my weak memory. may favorite is:

history | grep <search>
# or with ripgrep
history | rg <search>
Enter fullscreen mode Exit fullscreen mode

~/.bashrc

HISTSIZE=100000
HISTFILESIZE=200000

alias h="history | rg"
Enter fullscreen mode Exit fullscreen mode

so at the end just type (example) h commit I can found my latest commit - by the way I always use git from terminal because in this way git commands have memory, but this is true for every other bash command which I did.

Collapse
 
ricegf2 profile image
George F Rice

Control-r begins a "live" reverse search through bash history, updated as you type, and additional control-r presses will find the next matching command. When you find the command you want, edit it if needed and then Enter to execute.

Both control-r and "h" are useful in different ways. Control-r searches only from the beginning of the command, while "h" finds a match anywhere on the command line. And control-r is live and can repeat the search, and execute the result, while "h" statically lists all matching commands.

Collapse
 
a5537 profile image
A5537

Thank you for bringing up bash incremental searching! It is a handy tool, but there are a few things worth mentioning, so let me expand on your comment.

Both reverse-i-search Ctrl-R and forward i-search Ctrl-S (use with caution, this combination can send SIGSTOP and freeze your terminal; press Ctrl-Q to unfreeze) are not just searching, but also navigating in bash history like up/down arrows do.

If the current search line (original or edited) is executed with Enter or the search is aborted with Ctrl-G, then your position in bash history corresponds to the latest entry as expected. But if you start editing your search result or terminate the search with Ctrl-J or Esc, keep in mind that your position in history was moved back/forward to that line. That shifted position can be hundreds of entries away from your recent commands, so if up/down navigation or further searches are useless, you can Ctrl-C your way back or execute an empty line.

Also, at least in my experience, incremental searches are looking for a match in the whole string and not just in the beginning of it. However, this matching behavior may be different in your specific environment.

Collapse
 
george_ferreira profile image
George Ferreira

I would like to add the htop to the list. It is a command (I think it is not installed by default) similar to top but with extra options (and also colors :D).

Collapse
 
ramashankar_pandey_ profile image
Ramashankar Pandey

use glances command, it is more easier than top and htop. We have to install glances as it does not come by default.

Collapse
 
george_ferreira profile image
George Ferreira

I did not know this. It looks like pretty nice, I will try it.

Collapse
 
angelomandato profile image
Angelo Mandato

Add "locate", and it's update tool "updatedb". Combining with grep you can find files.

"cat" and others have a z variation, e.g. "zcat". You can open gz files now days with vim/vi.

It may be good to know ,"bash", "ln", "gcc", "perl", "php", "node", python, etc.. sometimes the easiest script you can make is in a language like PHP.

Collapse
 
josephj11 profile image
Joe

FYI: sudo is gradually being depreciated because it has a large attack surface. AFAIK, it is being replaced by doas. It's not quite as versatile, but it handles all common usages with something like 10% of the source code of sudo.

Collapse
 
litlyx profile image
Antonio | CEO at Litlyx.com

So useful to have cheat sheet on useful commands from basic to pro on linux. Keep up the good work man.

Antonio, CEO at Litlyx

Collapse
 
dev-nnamdi profile image
Nnamdi Kenneth Nwosu

Thanks

Collapse
 
kanani_nirav profile image
Kanani Nirav • Edited
Collapse
 
minato_1313 profile image
Vishal Morchhle

Helpful πŸ™‚

Collapse
 
lsgw31 profile image
Lucas Grenier

"Certainly!" Definitely not ChatGPT

Collapse
 
ookamisensei profile image
Goran Tomasic

Why does it say "Certainly!" as the first thing? Too lazy to remove it from the AI response before copy/pasting it as a post here? 😁

Collapse
 
martinbaun profile image
Martin Baun

Just saw an article about why AI-generated articles do really well on here. Very interesting.