Here it is, several usage of /dev/null special file.
We all know that /dev/null acts like a black hole in the universe. Anything you throw at it would be totally gone forever. Any attempts to read or write from it result to nothing. This means any data written to this /dev/null is just discarded and gone. This also mean that any data reads from this special file always returns end of file (EOF).
However, /dev/null can be quite useful in several ways too. Do not issue any commands mentioned here unless you know what you are doing. You've been warned. However, you can continue to read more from below.
How to zap a file without changing file ownership and permission?
From below sample, bigfile is owned by owner1, and you are root, truncating bigfile to a zero sized file without any to file permission and file ownership changes can also be done as follows.
# ls -la bigfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-rw-rw-rw- 1 owner1 owner1 123123123123 2007-08-16 19:04 bigfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As root:
# cp /dev/null bigfile
# ls -la bigfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-rw-rw-rw- 1 owner1 owner1 0 2007-08-16 19:05 bigfile
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ofcourse, file time stamps would be modified.
Now, we know that cat command display file contents into your scree. Let us try to suppress stdout. As root
# cat bigfile.txt > /dev/null
would show you nothing.
This is applicable also with stderr
# rm bigfile.txt 2> /dev/null
(Press Enter)
which obviously gives you nothing.
Now, how about issuing them both at the same time like so
# cat bigfile.txt 2> /dev/null > /dev/null
Interpretion:
If bigfile.txt does not exist, there will be no error message output from your screen since we supressed stderr. If bigfile.txt does exist, the file contents will not be displayed to stdout as well. So, you get no output at all using the above command. One probable usage for the above usage could be integrated whe using crontab utility which was covered here.
Try file copying over or overwriting /dev/null and see how it works
# cp bigfile.txt /dev/null
Another alternative use.
We have discussed before of creating file without editing it is by using linux command touch. Instead of using touch to create a zero sized file, we can make use of /dev/null to attain the same file output like so
# cat /dev/null > newfile.txt
Thinking wider, logically, this means that any log messages supposedly to be written to, say /var/log/secure can be zapped and suppressed as well, perhaps by an already inside hacker.
Well, it is ONLY here as further usage combination example. As root
# cp /dev/null /var/log/secure
# rm -rf /var/log/secure
# ln -s /dev/null /var/log/secure
Any future security messages supposedly to be logged to /var/log/secure would now be thrown to black hole (/dev/null) using the above command.
This is a bad practice and only here to elaborate more usage and security features that would be possible using /dev/null linux command.
Here's another one.
Let us say you have just downloaded a package installer. You have installed this package before with your previous work or company. And you are very aware that the package when installed asks too many questions answerable yes or no, or with default values.
And you know you'll just going to press the ENTER key for these question, /dev/null can be helpful here as well.
Execute the package or script like so
# ./new-program < /dev/null &
# ./new-program.sh < /dev/null &
If you happen to delete your /dev/null file, and you wish to recreate it back, simply follow here.
NOTES: Linux manual says, if /dev/null file is not writable and readable for all users, several application would act strangely.
If you think this /dev/null a.k.a. black hole has been interesting and useful for you, you might be interested with the black box from the middle of your screen as well. :)
Subscription
Categories
- HowTos (612)
- Linux Devices (40)
- Linux Diggs (620)
- Linux News (1541)
- Linux Videos (24)
Recent Posts
Blog Archive
-
▼
2007
(340)
-
▼
August
(95)
- KCron - GUI task scheduler
- Linux backups powered by Tar
- INQ7 front page image retrieval
- using wget for data and file transfers
- BibleTime - Bible study from Linux howto
- human readable DVD/CD drive technical details
- sound-juicer - alternative audio CD ripper install
- Stellarium - watch the sky from Linux
- Munin - monitor linux hosts install howto
- blocking yahoo chat messenger
- string manipulation using cut linux command
- graphing skystream DVB receiver's Eb/No and signal...
- BZFlag - 3D multi-player tank game install howto
- screenshot and snapshot creations howtos
- string parsing using bash
- grep multiple character from string or file
- enable and disable of telnet service
- grep multiple strings from a file
- remove spaces from filenames
- ISO creation and CD/DVD burning from terminal
- send a message to user's terminal
- retrieve GMail emails via terminal using fetchmail
- more of activating and deactivating network card
- set new mysql password
- TIP: enable thumbnail display images from apache
- monitor large mailbox users
- using the linux yes command
- string manipulation using tr linux command
- install and play 2D chess game in linux
- more firefox tips and tricks
- recover root password on linux
- establish ssh connection from different port
- uniq linux command
- remove blank lines using grep or sed
- date and time sync via NTP server howto
- who am I
- delete spam email and folder regularly howto
- hello world bash and perl script
- passwordless rdesktop session with XP howto
- force VGA screen resolution and screen mode
- RealPlayer 10 for linux install howto
- Grip - CD ripper install howto
- Banshee - music management and playback
- gnome music applet install howto
- Pirut and yum-updatesd - software management
- Alacarte - editing panel menus install howto
- access NTFS drive in Fedora
- FileLight - graphical disk usage and statistics
- TestDisk- partition tool install howto
- using /dev/null in linux terminal
- yahoo messenger in fedora install howto
- check and repair MS-DOS file systems howto
- using fdformat and mkdosfs from terminal
- Tremulous - Quake 3 install howto
- block consecutive IP address using scripts
- using floppy linux command from terminal
- display word or text file in reversed pattern
- convert a file to equivalent hex/binary code
- spell check text file from terminal
- create screen timer from linux howto
- recreate deleted /dev/null
- harddisk monitoring using smartctl
- bind ssh to selected IP address
- restrict su command to superuser only
- thunderbird install howto
- dovecot POP3/POP3S server with SSL/TLS install howto
- qpopper POP3 server install howto
- my other linux pages
- more ssh log parsing and monitoring
- checking daemon service bash script
- HTML CHM help file viewer install howto
- du - the disk usage linux command howto
- gnome language translator install howto
- display linux memory information howto
- display the number of processor howto
- 3d tabletennis game install howto
- Nokia N70 on Fedora via USB data cable
- Fedora 7 as guest host from VirtualBox
- at - jobs scheduling howto
- Nokia 70 linux connection via bluetooth dongle howto
- crontab - jobs scheduling howto
- managing daemon services howto
- create your own linux OS distro howto
- kernel devel headers install howto
- more multimedia browser plugins install howto
- numlock on with X install howto
- Fedora and RHEL differences
- create virtual terminals with single ssh connection
- virtual CentOS using VMWare 5.5.4 install howto
- VMware workstation 5.5.4 install howto
- 50 quick linux command tips part 4
- 5 SysAds permanent static route story
- ssh log parsing and monitoring
- removable drives, devices and media preferences
- gnome-blog desktop blogging install howto
-
▼
August
(95)
Thursday, August 16, 2007
using /dev/null in linux terminal
Subscribe to:
Post Comments (Atom)
ILoveTux - howtos and news | About | Contact | TOS | Policy
0 comments:
Post a Comment