My Linux tips and tricks page part 1. Many useful commands for Ubuntu and Linux Mint.

  1. My Linux tips and tricks page, Part 1.
  2. Getting started with Linux
  3. Setting up your BASH prompt.
  4. Using the finger command to list all logged in users
  5. Mounting an improperly shut down Windows XP installation with Knoppix 6.4 Linux
  6. Suspend to disk in Fedora Core 15 with the command-line
  7. Killing a running program in BASH
  8. Using Linux as your alarm clock
  9. Recording TV with VLC
  10. Updating OpenSuse 11.4
  11. Useful Linux commands

My Linux tips and tricks page, Part 1.

Go to part 2.

Getting started with Linux

The Linux virtual terminal in action.
The Linux virtual terminal in action.

Getting started with Linux for the first time is intimidating if you want to use the Linux command-line and you do not know the commands to use the prompt to its full potential. The best way to get acquainted with the Linux system after installation is to open the Gnome terminal or the Konsole and type man intro. An excerpt from this manual page is shown below. This manual page will give you a quick introduction to the BASH shell or Bourne Again Shell, this is the default shell prompt for Linux, there are others such as the sh shell, as well as Ksh, Zsh, and Csh, but the Bash shell is the easiest and most common shell used by Linux distributions. If you wish to type a command such as cat myfile.txt you do not need to type the whole command. You may type: cat my[TAB], ie type cat my then press the TAB key to auto-complete the command. Once you get used to using the TAB key to auto-complete the commands you are typing on the Linux command line the process will become very fast indeed. Using TAB will show a list of possible completions of the command making it clear what you need to type. You may also use wild-cards, ie typing ls DS* in a folder full of files will show a listing of all of the digital camera pictures that are in the folder. or ls -l *.jpg to list all of the jpg files in that folder.

The man intro command will print an introduction to the Linux shell. There is a hypertext copy of this manual page here: http://linux.die.net/man/1/intro.

This is a very good way to learn the basic commands to navigate around your UNIX/Linux system with the command line. And my Linux PDFs here: http://www.securitronlinux.com/lc. You can even type man man to learn about the man command and how it works. A manual page that has 1 at the end, for example, ls(1) is a command manual page. A manual page such as printf(3) is a manual page for a programming function. Sometimes a command and a function like printf() share the same name, in this case, type man 3 printf to access the desired manual page.

To list a folder like in DOS, type ls -la to list the files vertically. If you type ls -hula, then the file listing will use kilobytes and megabytes instead of bytes in the filesize column.

Creating a zip file containing a file with the Linux command line is very easy. Just use the zip command that should be included with your Linux distribution. The -8 parameter is to increase the level of compression applied to the file. You can use -9 for even more compression.

john@deep-thought:~/Documents$ zip -8 phobos3.zip phobos3.wad
updating: phobos3.wad (deflated 64%)

For systems based on Debian, after installation, type sudo apt update, and then sudo apt upgrade to upgrade your Linux system. When a user is using the command line, type man to get information about a command. For example, man ls will help you understand the ls command.

Apt and Dpkg tutorial for Debian based systems: http://www.securitronlinux.com/lc/13_apt_dpkg_tutorial.pdf.

Linux Pocketbook 2003: http://www.securitronlinux.com/lc/pocketbook2003.pdf. A great guide for anyone who wants to learn Linux and make the best use of their new Linux distribution.

Advanced Linux Pocketbook 2003: http://www.securitronlinux.com/lc/AdvancedLinuxPocketbook.pdf. Even more information for those of you who are wanting to learn Linux.

Debian Handbook PDF: http://www.securitronlinux.com/lc/debian-handbook.pdf.

To learn more about Linux, read manual pages for commands, type man man to get more information on the manual pages and their usage. The info man command will also provide a lot of very useful information.

To clear the console after logging out, use this in your ~/.bash_logout file.

# ~/.bash_logout: executed by bash(1) when login shell exits.
 
# when leaving the console clear the screen to increase privacy
 
if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

To get your IP address(es), use this command: ip a. This will work very well. Below is a better example.

.4 Tue Jan 28 jason@Yog-Sothoth 2: $ ip a | awk '$1 == "inet" && $2 != "127.0.0.1/8" { print $2, $3, $4; }'
192.168.1.2/24 brd 192.168.1.255
172.17.0.1/16 brd 172.17.255.255
192.168.122.1/24 brd 192.168.122.255

This prints all of the IP addresses on your Linux system if you have more than one. I am running KVM so I have a couple of virtual addresses too. This will exclude the localhost loopback address.

Setting up your BASH prompt.

A nice BASH shell prompt. This shows the current time and the current folder you are in as well as a full directory path if you are in a subfolder.

PS1="-\t-- \u@\h [\w]\$ "

How to set the contents of the Xterm title bar. Using the PROMPT_COMMAND variable. This displays the current directory path in the titlebar. Very useful indeed.

# Setting the value of the Xterm title.
# Only if we are in X...
if [ $DISPLAY ] ; then
	PROMPT_COMMAND='echo -ne "\033]0;${OSRELEASE}: ${PWD}\007"'
fi

How to find out what your IP address is. Using ifconfig and awk.

sudo /sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' | sed -e s/addr://

More useful variables to have in your ~/.bashrc.

# Setting a blinking block cursor for the console.
echo -e '\033[?6c'
 
# try to set DISPLAY smart (from Hans) :)
# From a SUSE .bashrc file.
#
if test -z "$DISPLAY" -a "$TERM" = "xterm" -a -x /usr/bin/who ; then
	WHOAMI="`/usr/bin/who am i`"
	_DISPLAY="`expr "$WHOAMI" : '.*(\([^\.][^\.]*\).*)'`:0.0"
	if [ "${_DISPLAY}" != ":0:0.0" -a "${_DISPLAY}" != " :0.0" \
		-a "${_DISPLAY}" != ":0.0" ]; then
		export DISPLAY="${_DISPLAY}";
	fi
	unset WHOAMI _DISPLAY
fi
 
export EDITOR=mcedit
export TIME_STYLE=+" %d-%m-%y %I:%M %P"
# These two examples from bash-doc.
export PAGER="/usr/bin/less"
export VIEWER="mcedit"
export LESS="-i -e -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-..."
shopt -s checkwinsize
 
[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"

Using the finger command to list all logged in users

This is easily achieved at the terminal prompt. Just type: finger $LOGNAME and the command will list all logged-in users. See the example below.

john@deep-thought ~ $ finger $LOGNAME
Login: john           			Name: John Cartwright
Directory: /home/john               	Shell: /bin/bash
On since Mon Jan 30 21:15 (EST) on tty2    1 day idle
On since Mon Jan 30 21:15 (EST) on tty3    1 day idle
On since Tue Jan 31 22:42 (EST) on pts/5 from :0.0
No mail.
No Plan.
john@deep-thought ~ $

You may also put this in your ~/.bashrc and this will print this information every time you open a terminal window.

if [ -x /usr/bin/finger ] ; then
   INFO=$(finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since" )
else
   INFO=$(uname -msov)
fi
 
echo -ne "${INFO}\n"

Mounting an improperly shut down Windows XP installation with Knoppix 6.4 Linux

I recently found an old hard disk with a Windows XP installation on it and I was trying to mount it with the GUI file manager, but I kept getting errors that the disk had a hibernation file on it and I could not mount the drive. I found out that I had to use this command to mount the disk.

root@Microknoppix:/home/knoppix# mount -t ntfs-3g -o remove_hiberfile /dev/sdd1 /media/sdd1

This command repaired the errors in the file-system and mounted the drive allowing me to access the contents.

Suspend to disk in Fedora Core 15 with the command-line

Suspending your machine from the command line in Fedora Core 15 is very easy with the pm-suspend command. this command requires root privileges but this is no real problem and this means that you do not need a GUI to suspend your machine.

root@localhost~# pm-suspend

Killing a running program in BASH

To kill a running program in the BASH shell you must first find the PID of the running application:

14:28:58-thx@matrix Documents >$ pidof bash
9032 8609 8441 6547 1348

or this way… Using ps and grep to find all running instances of BASH.

14:29:10-thx@matrix Documents >$ ps ax | grep bash
 1348 ?        S      0:00 /bin/bash /usr/sbin/ksmtuned
 6547 tty2     Ss+    0:00 -bash
 7378 ?        Ss     0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/startkde"
 8441 pts/1    Ss     0:00 bash
 8609 pts/1    S      0:00 bash
 9032 pts/2    Ss     0:00 bash
 9062 pts/2    S+     0:00 grep --color=auto bash
14:29:51-thx@matrix Documents >

Once you know the PID of the program that you want to kill, you may use the kill command to get rid of that running program.

kill 9032

or if it is a stubborn program that will not die you will need to be more persuasive:

kill -9 9032

Using Linux as your alarm clock

Just copy these two lines shown here into your crontab using the crontab -e command and then copy an mp3 file into your home folder and name it to match the file referred to in this file and it will play automatically in the morning as your alarm clock. Using mplayer is fine, it can run without any display and play music as well as movies. The crontab -l command allows you to view the crontab and review the contents to make sure they are correct.

[12:31:20-*-homer@hungry-forest ~]$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
 
30 8 * * * mplayer "/home/homer/sun.mp3"
 
40 8 * * * mplayer "/home/homer/sun.mp3"

Recording TV with VLC

VLC settings panel.

I was wanting to record television from my Compro T-300 video capture card and I could not get the sound to record properly from the line-in port. There would just be silence when the file was played back. I have the volume muted in alsamixer for the line-in port and when the VLC program is accessing the TV card I can still hear the sound, I found out that the volume setting as pictured in the screenshot to the linked above had to be a non-negative setting to be able to record any sound. Very simple once you know how to do it but it had me stumped for ages working this out. I probably could have the volume set to stereo instead of mono, but that does not matter, now I can record sound to my video files and record TV programs to watch again later.

Windows has myriad software options to record TV of varying quality, and it is about time that Linux had comparable software options that can allow you record television programs and you can also play a DVD or an Xvid file in VLC and press the red record button and it will capture the video to a video file. That is useful for capturing a clip from a movie to another file that you can put up on Youtube and show to the world. The Blaze DTV software for Windows is a good example of video software that can record from a webcam or video capture card. It saves as a mpeg file and VLC saves to an avi file, but the file sizes are huge and you need a lot of disk space to be able to store the files, but mencoder can be used to transcode the files to a more compressed format to save space.

Very long Linux uptime. This is Mandrake Linux 10.
Very long Linux uptime. This is Mandrake Linux 10.

Due to the flexibility of Linux, this is easily achieved with free software. I have purchased the Blaze DTV 6.0 software before when I was running on Windows XP, but I prefer the freedom and stability of Linux and UNIX over Windows these days. There are many and varied malware and spyware attacks directed at Windows and it is better to run something more secure and reliable over an insecure and attack prone operating system with a built-in web browser, Internet Explorer that in its version 8.0 incarnation is just as vulnerable to attack as the IE 6.0 web browser. The Firefox web browser should always be installed and only use Internet Explorer once to visit http://www.mozilla.org and download a proper and more secure web browser that will not be a conduit for every bit of malware under the sun. The Internet Explorer web browser has always been a thorn in the side of the Internet and if we got rid of that unfixable heap of code the Internet and its denizens would be better off indeed.

A Mac computer is very good though, as you can use a UNIX terminal emulator application and ssh, which means you can log into remote servers with it and update your website. The Mac gets a bad rap, but I actually like them as they run a variant of the UNIX BSD system and they are stable and reliable which is more than can be said about Windows.

Updating OpenSuse 11.4

To do a proper update of OpenSuse 11.4 you can type these commands.

sudo zypper ref && sudo zypper up

But typing sudo zypper dup will make sure all of the updates are installed, like typing sudo apt-get dist-upgrade on Ubuntu or Debian.

Useful Linux commands

Reading just the start or end of a file.

When you just want to see the start or end of a file this command will help.

head -n 40 data.txt

This will output the first 40 lines of the file.

tail -n 40 data.txt

will output the last 40 lines.

2 thoughts on “My Linux tips and tricks page part 1. Many useful commands for Ubuntu and Linux Mint.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.