this post was submitted on 25 May 2025
94 points (97.0% liked)

Linux

54348 readers
377 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS
top 9 comments
sorted by: hot top controversial new old
[–] sxan@midwest.social 4 points 3 hours ago

This is a good list.

There are three kinds of Linux commands:

  • commands I use frequently
  • commands I've never seen or don't know about. There's almost nothing in standard POSIX that falls in this category, and a lot of OSS that does. E.g., I use to always reach for fuser until I realized it's not a base install on many distros, so I switched to lsof which is is, and is also both more powerful and harder to use.
  • commands I've seen before but use so infrequently I forget they exist, or what they're called. This is sadly a larger set than I'd like.

Some of these in this list are the third kind.

[–] scrion@lemmy.world 8 points 5 hours ago

Most people will run a post 2.6 kernel, so prlimit will be available as an interesting alternative to ulimit.

[–] bizdelnick@lemmy.ml 1 points 3 hours ago

The most interesting command for the Linux shell is known as Barmin patch.

[–] vort3@lemmy.ml 6 points 7 hours ago (1 children)

Looks like the site is down or blocked in my country.

Could anyone please be so nice and copy paste those commands here?

[–] ClusterBomb@lemmy.blahaj.zone 35 points 6 hours ago (1 children)
  1. Supervise command (run every 2s)
watch "ls -larth"
  1. Kill program using one port
sudo fuser -k 8000/tcp
  1. Limit memory usage for following commands
ulimit -Sv 1000       # 1000 KBs = 1 MB
ulimit -Sv unlimited  # Remove limit
  1. Rename selected files using a regular expression
rename 's/\.bak$/.txt/' *.bak
  1. Get full path of file
readlink -f file.txt
  1. List contents of tar.gz and extract only one file
tar tf file.tgz
tar xf file.tgz static
  1. List files by size
ls -lS
  1. Nice trace route
mtr google.com
  1. Find files tips
find . -size 20c             # By file size (20 bytes)
find . -name "*.gz" -delete  # Delete files
find . -exec echo {} \;      # One file by line
./file1
./file2
./file3
find . -exec echo {} \+      # All in the same line
./file1 ./file2 ./file3
  1. Print text ad infinitum
yes
yes hello
  1. Who is logged in?
w
  1. Prepend line number
ls | nl
  1. Grep with Perl like syntax (allows chars like \t)
grep -P "\t"
  1. Cat backwards (starting from the end)
tac file
  1. Check permissions of each directory to a file

It is useful to detect permissions errors, for example when configuring a web server.

namei -l /path/to/file.txt
  1. Run command every time a file is modified
while inotifywait -e close_write document.tex
do
    make
done
  1. Copy to clipboard
cat file.txt | xclip -selection clipboard
  1. Spell and grammar check in Latex
detex file.tex | diction -bs

You may need to install the following: sudo apt-get install diction texlive-extra-utils.

  1. Check resources' usage of command
/usr/bin/time -v ls
  1. Randomize lines in file
cat file.txt | sort -R
cat file.txt | sort -R | head  # Pick a random sambple

# Even better (suggested by xearl in Hacker news):

shuf file.txt
  1. Keep program running after leaving SSH session

If the program doesn't need any interaction:

nohup ./script.sh &

If you need to enter some input manually and then want to leave:

./script.sh
<Type any input you want>
<Ctrl-Z>          # send process to sleep
jobs -l           # find out the job id
disown -h jobid   # disown job
bg                # continue running in the background

Of course, you can also use screen or tmux for this purpose.

  1. Run a command for a limited time
timeout 10s ./script.sh

# Restart every 30 minutes

while true; do timeout 30m ./script.sh; done
  1. Combine lines from two sorted files
comm file1 file2

Prints these three columns:

Lines unique to file1.
Lines unique to file2.
Lines both in file1 and file2.

With options -1, -2, -3, you can remove each of these columns.

  1. Split long file in files with same number of lines
split -l LINES -d file.txt output_prefix
  1. Flush swap partition

If a program eats too much memory, the swap can get filled with the rest of the memory and when you go back to normal, everything is slow. Just restart the swap partition to fix it:

sudo swapoff -a
sudo swapon -a
  1. Fix ext4 file system with problems with its superblock
sudo fsck.ext4 -f -y /dev/sda1
sudo fsck.ext4 -v /dev/sda1
sudo mke2fs -n /dev/sda1
sudo e2fsck -n <first block number of previous list> /dev/sda1
  1. Create empty file of given size
fallocate -l 1G test.img
  1. Manipulate PDFs from the command line

To join, shuffle, select, etc. pdftk is a great tool:

pdftk *.pdf cat output all.pdf        # Join PDFs together
pdftk A=in.pdf cat A5 output out.pdf  # Extract page from PDF

You can also manipulate the content with cpdf:

cpdf -draft in.pdf -o out.pdf      # Remove images
cpdf -blacktext in.pdf -o out.pdf  # Convert all text to black color
  1. Monitor the progress in terms of generated output

# Write random data, encode it in base64 and monitor how fast it is being sent to /dev/null

cat /dev/urandom | base64 | pv -lbri2 > /dev/null

# pv options:
#   -l,  lines
#   -b,  total counter
#   -r,  show rate
#   -i2, refresh every 2 seconds
  1. Find packages that have a given file in Ubuntu
apt-file update
apt-file search dir/file.h
[–] vort3@lemmy.ml 3 points 4 hours ago

Thank you very much!

[–] naught101@lemmy.world 4 points 7 hours ago (1 children)
[–] eugenia@lemmy.ml 8 points 7 hours ago

...and renice.

[–] deathbird@mander.xyz 11 points 10 hours ago

Actually yeah, those are.