Linux Tips – Pack I

This is the introductory post of a series of postings, which will contain various tips about Linux. All these tips have been collected through the years from various sources on the internet and were kept in numerous text files in my home directory. Others are my own “discoveries”. Recently, I moved them to Tomboy, but then I changed my mind and decided to put them on my blog to make them publicly available. Unfortunately, I haven’t kept a note of the source of each tip. So, please, if you happen to know what source had first published any of them, drop me a line. All these tips may be known to me or any medium-and-up experienced user, but could be useful for others. I tried to make a selection for this first post.
OK, this is enough for a prologue, so here we go…

Extract The Contents Of An RPM Package

This is how to extract the contents of an RPM package on the command line:

# rpm2cpio filename.rpm | cpio -i --make-directories

Patch Source Code

Put the patch in the directory with the program’s exploded source code tree and:

# patch -p1 -b < some_app.patch

The -b parameter, takes a backup of the original file, while the -p# (where # a number) strips one parent directory of the path where the patch would be applied if the -p0 flag was used. I hope you get the idea.

Execute A Command As Root

How to execute a command as user root with one line of code:

# su -c "make install"

You will be asked for the root’s password of course.

Download A Website

One of the available alternatives is to use the wget utility:

# wget -r www.example.com

There are a lot more flags that can customize the mirroring operation, but check the wget man page for this.

Reset Program’s Permissions

If you mess with each application’s permissions and you forget which files you have modified, here is a simple way to compare the permissions on all of the program’s installed files with the default ones, as they they were stored in the RPM database when the application was installed.

# rpm -Va avidemux

This only informs you about the files with modified permissions. To actually reset them, issue the following commands as root:

# rpm --setperms avidemux
# rpm --setugids avidemux

These should reset ownership/permissions back to what they should be.

Append A Line In A Text File

Too trivial:

# echo "Append this line at the end of document.txt" >> document.txt

Note the double “greater than” sign. If you use only one, then the document.txt will be re-written. So, take note.

Apply Patches In SPEC Files

Here is an example of patching the udftools package (probably this is from the official udftools SPEC file from Fedora Extras):

Source: http://dl.sf.net/linux-udf/udftools-%{version}.tar.gz
Patch0: udftools-1.0.0b3-pktsetup-chardev.patch
Patch1: udftools-1.0.0b3-mkudffs-bigendian.patch
Patch2: udftools-1.0.0b3-wrudf-gcc4.patch
Buildroot: %{_tmppath}/%{name}-%{version}-%{release}-root
.
.
%prep
%patch0 -p1 -b .pktsetup-chardev
%patch1 -p1 -b .mkudffs-bigendian
%patch2 -p1 -b .wrudf-gcc4

Test A Joystick

Here is how to test if a joystick works by using the console:

# cat /dev/input/js0

Now, press any joystick buttons, or move the stick and see that weird characters on stdout.

Re-Detect Your Hardware

Well, this seems to be specific to Fedora, because of the kudzu program. First rename the existing /etc/modprobe.conf and /etc/sysconfig/hwconf files:

# mv /etc/sysconfig/hwconf /etc/sysconfig/hwconf.bak
# mv /etc/modprobe.conf /etc/modprobe.conf.bak

And then have them re-created by running the kudzu program:

# kudzu

Log A Terminal Session

There is a BASH command, weirdly named script, which can log everything that is printed on stdout/stderr.

To start logging:

# script mylog.txt

To stop logging and have everything written to the mylog.txt file, simply press Ctrl-D (send the Escape character to the terminal) or simply type exit. There are some useful options, if you check the man page, that can append to the log file instead of overwriting, write the log file synchronously etc.

Quickly Backup Files

To create backups of all the files with the same extension in a directory with a single line of code, issue the following:

# for file in *.conf ; do cp $file $file.orig ; done

All *.conf files will be backed up with filenames of the form: *.conf.orig

To restore them:

# for file in *.orig ; do cp $file `basename $file.orig` ; done

If you want the *.orig files removed as well, consider using mv instead of cp.

Specify The Runlevel On-Boot

Some times it is necessary to boot into different runlevel from the one that is specified in /etc/inittab. You can do this from the boot manager.

In case you use LILO, add the runlevel number as a parameter. For example, to boot in single user mode (runlevel 1)

LILO Boot: linux 1

In case you use GRUB, move your selection to the kernel to load and press the ‘e’ key (editor). Now highlight the kernel line and press ‘e’ (editor) again. Goto the end of the line and add the runlevel number. For example:

kernel /boot/vmlinuz-2.6.7-1.custom ro root=LABEL=/ quiet 1

Now, press [Enter], and then ‘b’ to boot.

Get A List Of The Files An RPM Package Has Installed

This is so trivial…but anyway:

# rpm -ql packagename

Downgrade An RPM Package

A new version may have problems and break things. To downgrade sqlite for example:

# rpm -Uvh --oldpackage sqlite-3.2.8-1.i386.rpm

Mount An ISO File

You can mount an ISO file as if it was a volume by using the loop mount option, like this:

# mount -o loop -t iso9660 my.iso /mnt/tmp

The ISO image gets mounted at /mnt/tmp

Create An ISO Image File

This can be done by using the mkisofs utility. The following can be used for data CDs/DVDs:

# mkisofs -J -l -R -V "My CD Title" -o cdimage.iso files/

The contents of the files/ directory are included in the ISO image. The -J option also generates Joliet filenames in addition to regular iso9660 file names. -l allows full 31 character filenames, while -V sets a title for the CD/DVD.
Also, the -R option adds the Rock-Ridge extensions, so more filesystem info about the files is included.

For DVD video images, use the -dvd-video option, which generates a DVD-Video compliant UDF file system:

# mkisofs -dvd-video -V "My DVD Title" -o dvdimage.iso dvdfiles/

Burn An ISO Image

Use cdrecord for CD images:

# cdrecord -v gracetime=2 dev=/dev/hdc speed=24 driveropts=burnfree -eject cdimage.iso

Use growisofs for DVD ISOs:

# growisofs -dvd-compat -speed=8 -Z /dev/hdc=dvdimage.iso

Start An SSH Tunnel And Use It

This was not a very descriptive title. What the following line of code does is to create the SSH tunnel and start the application that will use it. The magic is that when the application is closed, the tunnel will go down as well, without having to kill it manually. The following example shows how to use vncviewer to connect through an SSH tunnel:

# ssh -f -L 25903:127.0.0.1:5903 leopard@server.example.com sleep 10; vncviewer 127.0.0.1:25903:3

For a detailed explanation, please read my relevant comment on my VNC article.

So, I guess that will be it for now. To be continued on Tip Pack II…

Linux Tips – Pack I by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2006 - Some Rights Reserved

George Notaras avatar

About George Notaras

George Notaras is the editor of the G-Loaded Journal, a technical blog about Free and Open-Source Software. George, among other things, is an enthusiast self-taught GNU/Linux system administrator. He has created this web site to share the IT knowledge and experience he has gained over the years with other people. George primarily uses CentOS and Fedora. He has also developed some open-source software projects in his spare time.

2 responses on “Linux Tips – Pack I

  1. Db0 Permalink →

    Nice tips but I think it would be best if you posted them separately and used appropriate tags for each.
    Just an idea.

    PS: I think the GFDL is a better license for such information instead of the creative commons

  2. George Notaras Post authorPermalink →

    I guess I was too lazy to create separate posts for each one of them, but certainly it would be better to have them properly tagged, so that one could locate them easily…

    You are right about the license. I hadn’t spent much time reading license terms when I started the blog. CC was the first I read about, it suited my needs, so I used that one. GFDL is a good option and I was also thinking about OPL. Hopefully, it will not require much manual editing, since the license info is added automatically by a plugin. :-)