LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-24-2020, 12:27 PM   #1
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Rep: Reputation: 24
Talking Let's talk about device files under /dev and their functions!


New linux user here. My brother introduced me to it a year or two ago and since I have shunned mac and Windows and only have Debian 10 or Kali on every computer I own.


My questions is this: What are some of the most interesting files in /dev and how can we make practical use of them??


I've already learned how to use /dev/zero with the dd command to basically completely wipe a block device like a hard drive. And I understand that you can use /dev/random to generate random numbers.

What, in ya'll's opinion are some other practical things that can be done with the device files in /dev?


Thanks in advance for the practical insight!


Arc1c_f0x

Last edited by Arct1c_f0x; 07-24-2020 at 01:11 PM.
 
Old 07-24-2020, 01:51 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
There's /dev/null -- the "bit bucket"
 
1 members found this post helpful.
Old 07-24-2020, 02:31 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
And /dev/random.

EDIT: Oops, already said.
 
Old 07-24-2020, 02:54 PM   #4
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Thanks, Scasey I'll start reading about /dev/null !



Hey, Dugan. You might know how I can use /dev/random in a bash script in a specific way.

Let me explain. I'm writing a .sh file called "Bedtime routine.sh". When I execute this file by typing sudo ./Bedtime routine.sh, the program will start, ask how long till I want the computer to shutdown and then play a selection of mozart at random from the mozart directory on my computer. I already completed the first part. My problem now is how might I use /dev/random to supply my program with some sort of value that selects a mozart piece at random from the specified directory?

This is what is looks like so far:

#!/bin/bash
# program that asks shutdown time and plays mozart file at random until shutdown.

echo "Bedtime Routine Program initiated"

echo "Time till computer shutdown?"

read shutdownTimeArg

sudo shutdown $shutdownTimeArg

echo "end of test"
 
Old 07-24-2020, 03:03 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
Basically device files i.e. those files in /dev are an interface to a device driver that looks like an ordinary file. They are used for keyboards, mice, hard drives, printers, legacy parallel or serial ports, audio, optical drives. tty terminals, framebuffers and so on.

Those special files like /dev/null are known as pseudo-devices
/dev/null, /dev/random, /dev/urandom, /dev/full or /dev/zero

With modern distributions /dev is a virtual filesystem that exists only in memory and created when the system boots.

The shuf command is an easy way to use random numbers.

Last edited by michaelk; 07-24-2020 at 03:09 PM.
 
Old 07-24-2020, 03:37 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
^ what does /dev/full do?

There's /dev/urandom, which is /dev/random (one cheesy phrase coming up) "on steroids".
There's /dev/zero, an endless fountain of zeros!
There's /dev/disk/by*, very useful.
...
....
.....
......
 
Old 07-24-2020, 03:49 PM   #7
Roken
Member
 
Registered: Oct 2011
Location: Warrington, UK
Distribution: Arch local, Debian on VPS, several RPIs.
Posts: 300
Blog Entries: 1

Rep: Reputation: 55
As an aside, don't write a script that requires "sudo" for something so trivial.

Instead, setup sudoers to allows the use access to whichever command requires sudo. It exposes far fewer vulnerabilities.
 
Old 07-24-2020, 04:09 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
/dev/full always returns error code ENOSPC i.e. no space left on device. It is used to test programs response to disk full errors.

Code:
echo "Hello World" > /dev/full
 
1 members found this post helpful.
Old 07-24-2020, 07:02 PM   #9
KGIII
Member
 
Registered: Sep 2015
Location: North-Western, Maine - in the mountains.
Distribution: Lubuntu 18.04 LTS
Posts: 158
Blog Entries: 6

Rep: Reputation: 64
Here's an informative link for you, from one of my favorite sites:

http://www.linux-databook.info/?page_id=5108
 
2 members found this post helpful.
Old 07-24-2020, 08:48 PM   #10
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Roken View Post
As an aside, don't write a script that requires "sudo" for something so trivial.

Instead, setup sudoers to allows the use access to whichever command requires sudo. It exposes far fewer vulnerabilities.
What do you mean exactly? That I run the script using sudo as a prefix: "sudo ./Bedtime Routine" , instead of including it in the .sh file itself?

I don't know whether this matters or not with regard to what you said but I am the only user on the aformentioned desktop
 
Old 07-24-2020, 08:49 PM   #11
Arct1c_f0x
Member
 
Registered: Feb 2020
Posts: 123

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by KGIII View Post
Here's an informative link for you, from one of my favorite sites:

http://www.linux-databook.info/?page_id=5108
Thanks, KGIII. That was a good read
 
Old 07-24-2020, 09:10 PM   #12
KGIII
Member
 
Registered: Sep 2015
Location: North-Western, Maine - in the mountains.
Distribution: Lubuntu 18.04 LTS
Posts: 158
Blog Entries: 6

Rep: Reputation: 64
Quote:
Originally Posted by Arct1c_f0x View Post
Thanks, KGIII. That was a good read
The site's a pretty informative site. It's one of my favorites. I'm still learning new stuff with Linux and that's one of the many awesome resources out there.

I should probably compile a list of the various sites I've found over the years. Hmm... Maybe I'll have time to do that in the near future.
 
1 members found this post helpful.
Old 07-24-2020, 09:18 PM   #13
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,735

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by KGIII View Post
The site's a pretty informative site. It's one of my favorites. I'm still learning new stuff with Linux and that's one of the many awesome resources out there.

I should probably compile a list of the various sites I've found over the years. Hmm... Maybe I'll have time to do that in the near future.
Sounds like something for the LQ Blog. I’m looking forward to it. 😉
 
Old 07-24-2020, 09:30 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
A quick go at your bedtime program. If you generate a text file with all of your desired music files from the desired directory.

Code:
music_files.txt
file1.mp3
file2.mp3

#!/bin/bash
readarray music < music_files.txt

num=${#music[@]}

select=$(shuf -i 0-$num -n 1)

play_me=${music[$select]}

#Use whatever app to play $play_me
 
1 members found this post helpful.
Old 07-24-2020, 11:53 PM   #15
KGIII
Member
 
Registered: Sep 2015
Location: North-Western, Maine - in the mountains.
Distribution: Lubuntu 18.04 LTS
Posts: 158
Blog Entries: 6

Rep: Reputation: 64
Quote:
Originally Posted by scasey View Post
Sounds like something for the LQ Blog. I’m looking forward to it. 😉
That's not a terrible idea. I've got a few blog articles, all but one written while probably drinking wine! I'm also not willing to maintain another blog for it - or dedicate a terrible amount of time to writing a site just for it. So, that's a good idea and I've got a ton of links. Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why is there a need for /dev/null, /dev/zero and other special device (files) in /dev suttiwit Linux - General 13 08-07-2012 12:26 AM
pseudo terminals: /dev/ptmx <-> /dev/pts/ vs. /dev/ptyp <-> /dev/ttyp rtspitz Linux - Software 2 12-02-2011 02:07 PM
device mapper / multipath creating extra device, won't let me talk to the one i want chakkerz Linux - Server 1 03-16-2008 05:52 PM
/dev/tty0, /dev/tty1, /dev/tty10...and so on...which should be used for a device ??? b0nd Slackware 2 04-02-2006 08:14 AM
why cannot "talk" to others,since mesg is yes and disable = no(in /etc/xinetd.c/talk) whepin Linux - Newbie 0 12-31-2001 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration