141
all 33 comments
sorted by: hot top controversial new old
[-] jim3692@discuss.online 31 points 3 weeks ago

You also need to ln -s /dev/null /dev/nul

[-] eager_eagle@lemmy.world 14 points 3 weeks ago
ln -s /dev/null /dev/nul
ln -s /dev/nul /dev/shhhhh
ln -s /dev/shhhh /dev/shhhhh
ln -s /dev/shhh /dev/shhhh
ln -s /dev/shh /dev/shhh
[-] nibbler@discuss.tchncs.de 19 points 3 weeks ago

nul is Windows/DOS.

[-] Kidplayer_666@lemm.ee 14 points 3 weeks ago
[-] korthrun@lemmy.sdf.org 19 points 3 weeks ago* (last edited 3 weeks ago)

This is a command that throws a permission denied error while trying to create a symlink to a file that almost certainly does not exist.

It's like someone turning to you and saying "Knick knack!" then waiting for you to ask "who's there?"

[-] jim3692@discuss.online 13 points 3 weeks ago* (last edited 3 weeks ago)

In bash, when you redirect the output of a command to /dev/null, like cat /etc/passwd >/dev/null, you are silencing the output.

There are cases that this is useful, for example when checking if an application is installed:

node -v >/dev/null && echo "Node.js is installed"

This line tries to get the version of Node.js, but it silences the output. That's because we don't care about the version. We only care about whether the execution was successful, which implies the existence of Node.js in the system.

[-] korthrun@lemmy.sdf.org 11 points 3 weeks ago* (last edited 3 weeks ago)

Dear linux newbies of the fediverse:

Please do not run cat for the sole purpose of copying a single files content to STDOUT

Your system almost certainly has a pager on it (e.g. 'less', 'more', 'most'). Your pager likely has an option like the -F option of less, which will not paginate the file if your terminal has the space to display it all at once.

You do not need to involve cat to get a files contents into a variable. Any POSIX compliant shell will support MYVAR=$(</tmp/myfile)

You do not need to involve cat to iterate over the lines of a file. You can do things like:

while read myline
do
    printf "found '%s'\n" "$myline"
done </tmp/myfile

If you want to concatenate multiple files, but do not care if they all exist, you might use /dev/null to suppress the "no such file" error from cat as such cat file1 file2 file3 2>/dev/null. Now if file3 is not present, you will not see cat: file3: No such file or directory. 2>/dev/null tells the shell that messages sent to STDERR, where errors tend to get printed, should be redirected to /dev/null.


Please do not invoke a command only to see if it is available in the directories listed your PATH environment variable

As an aside this is not the same as seeing if it's installed.

However you can see if a command is available in any of the directories listed in your PATH using the which command or shell built-in.

You might want to do something like:

#!/bin/bash

which node &> /dev/null
HAS_NODE="$?"

# ... MORE CODE HERE ...

if [[ $HAS_NODE ]]
then
    # something you only do if node is present
    :
else
    # do something else or print a friendly error
    :
fi

This way you don't see the output of the "which" command when you run the script, but you do get it's exit code. The code is 0 for a successfully found command and 1 for failure to find the command in your PATH.

[-] qqq@lemmy.world 5 points 3 weeks ago* (last edited 3 weeks ago)

Alternatively, use your shell however you want. And which isn't POSIX so I wouldn't use that in a shell script you intend to share.

[-] korthrun@lemmy.sdf.org 1 points 3 weeks ago* (last edited 3 weeks ago)

Once upon a time I would have been more particular about the "which issue". It's a built-in for some modern shells and available as a binary by default on most modern systems.

You are correct though, if you want to write a 100% POSIX compliant shell script you're better off using command, type or actually looping over the contents of $PATH and checking for the presence of your desires binary.

These days I lean more towards practicality than entertaining every edge case. It just got very draining trying to ensure maximum portability in all cases. Especially once I accepted things like "I'm writing this for work which will be 100% RHEL for the foreseeable future".

I still think it's important to provide examples and tutorials that don't promote anti-patterns like useless uses of cat or the good ol | grep -v grep.

[-] Colloidal@programming.dev 5 points 3 weeks ago

Now I'll do it even harder!

[-] korthrun@lemmy.sdf.org 2 points 3 weeks ago

HECK YEAH! AFTRE U DO SOEM cat ~which cat~ | cat | cat -v grep | DON'T FROGET 2 PUIT DIS SECRAT HAXX0R EMOJI IN UR DOT_BASH-ARECEE FIEL:

:(){ :|:& };:

  • JEFFK
[-] Colloidal@programming.dev 3 points 3 weeks ago

Yeee haaww!

Happy cake day BTW.

[-] korthrun@lemmy.sdf.org 2 points 3 weeks ago

πŸ’• thanks!

[-] sem 2 points 3 weeks ago

Why would you do "less -F " when "cat " is easier to type, and reminds you of cats?

[-] korthrun@lemmy.sdf.org 2 points 3 weeks ago* (last edited 3 weeks ago)

So most importantly I'd add -F to the LESS environment variable. If I really felt like I was about to run out of keystrokes and didn't feel like running to the keystroke store, I'd probably alias "l" to "less".

That aside, you can use a hammer to push a screw into wood. You can use a screwdriver to beat a nail into a board. You can use a board to drive a dowel through a plank. The job gets done either way.

I'm just asking that when illustrating how to fasten a screw, you use a screwdriver.

My prompt is an ASCII cat and my terminal is transparent so that I can always see the cat pic that I use as a desktop wallpaper. Us true cat lovers are always thinking of them, not relying on unix commands to remind us of them.

Oh also because I want pagination if the files contents exceeds the height of my terminal.

[-] sem 2 points 3 weeks ago

I guess I still don't understand why?

The end result is that the contents of the file ends up in the STDOUT.

For your other examples, if you use a hammer to push a screw into wood, it won't be secure and it damages the wood. Using a board to drive a dowel through a plank might work in a pinch, but it is easier to use a hammer.

What is the bad thing that happens if you use cat for its side effect rather than to concatenate?

[-] korthrun@lemmy.sdf.org 1 points 3 weeks ago* (last edited 3 weeks ago)

I do not agree with the premise that there needs to be a negative repercussion to doing something before we look at examining the behavior.

I guess I could do some serious gymnastics and reach for something like "when a text file is longer than your terminal scrollback and you cat it, you lose history that you may have been expecting to reference".

Many of the sort of examples I'm referencing involve spawning subshells needlessly, forking/execing when it's not actually needed, opening file descriptors that otherwise wouldn't have been opened. We're in an interesting bit of the tech timeline here where modern computing power makes a lot of this non-impactful performance wise, but we also do cloud computing where we literally pay for CPU cycles and IOPS.

I guess I'm just a fan of following best practices to the extent practical for your situation, and ensuring that the examples used to inform/teach others show them the proper way of doing things.

No bad things happen when I pour a Hefe into a Pilsner glass either, but now the Germans are coming for me.

[-] sem 1 points 3 weeks ago* (last edited 3 weeks ago)

Thanks for the explanation, I was wondering if it had to do with CPU cycles.

I guess I'll continue to use cat for short files to sdout and less for longer files, if there is no actual repercussion. It's just such a common "don't do this" topic I was wondering if there was a good reason not to.

I think the beer in the "wrong" glass might be an apt metaphor -- it might be fancier to use a specific glass, knowing the history, appreciating the golden color of the beer, (it might also affect the head on the pour? Idk) but there is also nothing wrong with drinking it out of a normal glass.

Edit: I've never used view, but I have a distant memory of once using more instead of less.

[-] pelya@lemmy.world 13 points 3 weeks ago

The ingenuity of this command is that /dev/nul does not exist, the correct path is /dev/null, however the command executes without error and creates a symlink to a non-existing path.
The only thing missing is sudo.

[-] ConstantPain@lemmy.world 3 points 3 weeks ago

The font is Cascadia Code too.

[-] nibbler@discuss.tchncs.de 1 points 3 weeks ago

there are a million cases where sudo is not required.

[-] korthrun@lemmy.sdf.org 3 points 3 weeks ago* (last edited 3 weeks ago)

My dumbass can only come up with three:

  1. You are already root (ok, fine)
  2. You have made /dev/ writable by non-privileged users
  3. Your non-privileged user already owns the symlink /dev/nul. Which "ok, fine", but also the point of command would have to be to functionally do nothing other than print out the error ln: failed to create symbolic link '/dev/nul': File exists

I would love to understand the use case behind #2. I am also curious to see even 7 more cases, let alone your figurative million.

In regards to #3 even if the behaviour of ln was to replace a symlink if it already existed, it'll probably have to unlink() the existing symlink, which I'm pretty sure is gonna get you a permission denied error on any /dev filesystem with sane permissions.

[-] korthrun@lemmy.sdf.org 1 points 3 weeks ago* (last edited 3 weeks ago)

Follow up, tested and confirmed #3:

[korthrun@host]$ ls -l /dev/korth
.rw-r--r-- korthrun wheel 0 B Wed Jun 11 17:11:03 2025 /dev/korth
[korthrun@host]$ rm /dev/korth
rm: cannot remove '/dev/korth': Permission denied
[-] nibbler@discuss.tchncs.de 1 points 3 weeks ago

ln could be +s

the kernel could have been modified

I'm sure there is some way if using capabilities

you don't need to be 'root', uid 0 is enough :)

[-] massive_bereavement@fedia.io 3 points 3 weeks ago

ln -s /dev/null /dev/theresnodanaonlynull

[-] Colloidal@programming.dev 2 points 3 weeks ago

There's ono Dana only null?

[-] massive_bereavement@fedia.io 2 points 3 weeks ago

Oopsie, let's correct that and pretend it didn't happen.

this post was submitted on 11 Jun 2025
141 points (100.0% liked)

linuxmemes

26006 readers
769 users here now

Hint: :q!


Sister communities:


Community rules (click to expand)

1. Follow the site-wide rules

2. Be civil
  • Understand the difference between a joke and an insult.
  • Do not harrass or attack users for any reason. This includes using blanket terms, like "every user of thing".
  • Don't get baited into back-and-forth insults. We are not animals.
  • Leave remarks of "peasantry" to the PCMR community. If you dislike an OS/service/application, attack the thing you dislike, not the individuals who use it. Some people may not have a choice.
  • Bigotry will not be tolerated.
  • 3. Post Linux-related content
  • Including Unix and BSD.
  • Non-Linux content is acceptable as long as it makes a reference to Linux. For example, the poorly made mockery of sudo in Windows.
  • No porn, no politics, no trolling or ragebaiting.
  • 4. No recent reposts
  • Everybody uses Arch btw, can't quit Vim, <loves/tolerates/hates> systemd, and wants to interject for a moment. You can stop now.
  • 5. πŸ‡¬πŸ‡§ Language/язык/Sprache
  • This is primarily an English-speaking community. πŸ‡¬πŸ‡§πŸ‡¦πŸ‡ΊπŸ‡ΊπŸ‡Έ
  • Comments written in other languages are allowed.
  • The substance of a post should be comprehensible for people who only speak English.
  • Titles and post bodies written in other languages will be allowed, but only as long as the above rule is observed.
  • 6. (NEW!) Regarding public figuresWe all have our opinions, and certain public figures can be divisive. Keep in mind that this is a community for memes and light-hearted fun, not for airing grievances or leveling accusations.
  • Keep discussions polite and free of disparagement.
  • We are never in possession of all of the facts. Defamatory comments will not be tolerated.
  • Discussions that get too heated will be locked and offending comments removed.
  • Β 

    Please report posts and comments that break these rules!


    Important: never execute code or follow advice that you don't understand or can't verify, especially here. The word of the day is credibility. This is a meme community -- even the most helpful comments might just be shitposts that can damage your system. Be aware, be smart, don't remove France.

    founded 2 years ago
    MODERATORS