SED useful commands

Valic —  February 5, 2013 — Leave a comment

1. How to remove lines ending with ‘baddump’ from a text file:

You’ve got a file with a bunch of lines end with “baddump”  and you need those lines to be removed completely without leaving any blank lines behind? This is the command to do it:

sed -i '/baddump$/d' file

2. You have text files with tons of empty lines and you want to get rid of those in one second?

sed '/^$/d' file > new_file

You may have multiple html files to correct at the same time.You can do that with foreach command:

foreach file (*html)
sed '/^$/d' $file > new_files Continue Reading...

These are some other commands that you will find very useful:

 

Command 

Description

clear

Clear the screen. This command clears  your  screen if this is possible.

echo

Display text on the screen. Mostly useful when writing shell scripts.

more

More is a filter for paging through text one screenful at a time.

less

An improved replacement for the more command. Allows you to scroll backwards as well as forwards.

grep

Search for a pattern in a file or program output.B y default, grep prints the matching lines.

lpr

Print a file or program output. lpr uses a spooling daemon to print the named files when facilities become available.

sort

Sort a file or program output.  Writes sorted files concatenated.

su

su – Switch User is the command that allows you to switch to another user account.

The following commands can be used to:

  • find out information about files
  • display files
  • copy, move or delete files
Linux Command Command Description
file This command shows you out what kind of file it is.

Example:  file /bin/ls will tells you that it is a Linux executable file.

cat Display the contents of a text file on the screen. Example: cat filelist.txt will display the contect of the file filelist.txt
head Display the first 10 lines of a text file. Example: head /var/log/messages This will display the first 10 lines from the messages log file.
tail Display the last 10 lines of a text file. Example:  tail /var/log/messagesThis will display the last 10 lines from the messages log file.
tail -f Display the last 10 lines of a text file and then output appended data as the file grows.

Example: tail -f /var/log/messages

cp Copies a file from one location to another. Example: cp file.txt /tmp
Thos commadn copies the file.txt file to the /tmp directory
mv Moves a file to a new location, or renames it. For example: mv file.txt /tmp
rm Delete a file. Example: rm /tmp/file.txt
mkdir Make Directory. Example: mkdir /tmp/mydirectory
rmdir Remove Directory. Example: rmdir /tmp/mydirectory
NOTE: You can practice these commands by yourself and find more about all commands using the man command.

The Linux filesystem is a tree-like hierarchy of directories and files. The base of the filesystem is the “/” directory, also known as the “root” . Linux filesystem mounts all disks underneath the / filesystem, not like DOS or Windows filesystems that can have multiple roots. The following table shows you the most common Linux directories:

/bin

In this directory we will find essential command binaries like bash, ls, ps, tar, and others.

/boot

Here are static files of the boot loader.

/dev

In here we can find device files. In Linux hardware devices are accessed just like other files.

/etc

Here are system configuration files

/home

This is the location of users personal home directories. Continue Reading…

It is often the case that you have multiple terminals open on one system. With bash with default settings once you press the up button you can’t see the previous command entered on other terminal but you can see the commands entered on the same terminal. This problem is getting even worst. On default, bash history is  saved only for the last closed session.

Now if you want to save all bash history for all open sessions do the followings: Open the .bashrc file from your home directory and add the lines:

shopt -s histappend export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Continue Reading…

Page 2 of 41234