Archive

Posts Tagged ‘linux commands’

Linux – SCP multiple files / directories


It is possible to copy multiple source files / directories using secure copy, however it is not possible to copy to multiple destinations.

To do so you can use the following code:

$ scp -rp <source_filename1> <source_directory1> <destination>

Advertisements

Linux – Command Cheat Sheet

July 22, 2011 9 comments

A quick Linux cheat sheet, for those handly little commands you use all the time but sometimes your brain freezes up and a quick reference would be good to have:

File Commands

ls                       -list directory
ls -al                   -formatted listing with hidden files
ls -ltr                  -formatted listing ordered by time
cd dir                   -change to directory
cd -                     -go to directory in previously
cd                       -go to home directory
pwd                      -show current directory path
mkdir dir                -make directory dir
rm file                  -delete file
rm -r dir                -delete directory dir
rm -f file               -force remove file
rm -rf dir               -force remove directory dir
cp file1 file2           -copy file1 to file2
cp -r dir1 dir2          -copy dir1 to dir2; create dir2 if it doesn’t exist
mv file1 file2           -rename/move file1 to file2 if file2 is an existing dir, moves file1
into dir file2
ln -s file link          -create symbolic link link to file
touch file               -create or update file
cat > file               -places standard input into file
more file                -output the contents of file
head file                -output the first 10 lines of file
tail file                -output the last 10 lines of file
tail -f file             -output the contents of file as it grows, starting with the last 10 lines

Process Management

ps                       -display your currently active processes
top                      -display all running processes
kill pid                 -kill process id pid
killall proc             -kill all processes named proc
bg                       -lists stopped or background jobs; resume a stopped job in the background
fg                       -brings the most recent job to foreground
fg n                     -brings job n to the foreground

File Permissions

chmod octal file         -change the permissions of file to octal, which can be found separately for user, group, and world by adding:
● 4 – read (r)
● 2 – write (w)
● 1 – execute (x)
Examples:
chmod 777                -read, write, execute for all
chmod 755                -rwx for owner, rx for group and world For more options, see man chmod.
chown user:group file    -change the owner and group for a file

SSH

ssh user@host            -connect to host as user
ssh -p port user@host    -connect to host on port port as user
ssh-copy-id user@host    -add your key to host for user to enable a keyed or passwordless login

Searching

grep pattern files       -search for pattern in files
grep -r pattern dir      -search recursively for pattern in dir
command | grep pattern   -search for pattern in the output of command
locate file              -find all instances of file
find . file              -find file within current directory
find / file              -find file within whole system

System Info

date                      -show the current date and time
cal                       -show this month’s calendar
uptime                    -show current uptime
w                         -display who is online
whoami                    -who you are logged in as
finger user               -display information about user
uname -a                  -show kernel information
cat /proc/cpuinfo         -cpu information
cat /proc/meminfo         -memory information
man command               -show the manual for command
df                        -show disk usage
du                        -show directory space usage
du -sk | sort -n          -show sum of each directory space usage sorted by number in KB
free                      -show memory and swap usage
whereis app               -show possible locations of app
which app                 -show which app will be run by default

Compression

tar cf file.tar files     – create a tar named file.tar containing files
tar xf file.tar           extract the files from file.tar
tar czf file.tar.gz files – create a tar with Gzip compression
tar xzf file.tar.gz       – extract a tar using Gzip
tar cjf file.tar.bz2      – create a tar with Bzip2 compression
tar xjf file.tar.bz2      extract a tar using Bzip2
gzip file                 – compresses file and renames it to file.gz
gzip -d file.gz           – decompresses file.gz back to file

Network

ping host                 – ping host and output results
whois domain              – get whois information for domain
dig domain                – get DNS information for domain
dig -x host               reverse lookup host
wget file                 download file
wget -c file              continue a stopped download

Installation

Install from source:
./configure
make
make install
dpkg -i pkg.deb           – install a package (Debian)
rpm -Uvh pkg.rpm          – install a package (RPM)

Shortcuts

Ctrl+C                    – halts the current command
Ctrl+Z                    stops the current command, resume with fg in the foreground
or bg in the background
Ctrl+D                    log out of current session, similar to exit
Ctrl+W                    erases one word in the current line
Ctrl+U                    erases the whole line
Ctrl+R                    type to bring up a recent command
!!                        - repeats the last command
exit                      log out of current session

Linux – Symbolic Links

July 13, 2011 7 comments

With Unix/Linux you can create links to directories/files using a symbolic link. A symbolic link is different to a hard link.

Hard Link vs. Symbolic Link

  • A hard link cannot link to a directory or different filesystem/volume, and must link to an existing file.
  • A symbolic link can link to a directory, different filesystem/volume, and can also link to an arbitary value that may not exist.

Create a hard link

To create a hard link you can issue the following from the command line:

$  ln source_file link_name

This creates the link from source_file to link_name. The first file is always the name of the file you want to create a link to. The second file is always the name of the link that you wish to create.

Create a symbolic link

A symbolic link is created in much the same way, however we issue the -s flag.

$  ln -s source_file link_name

Flags

-f, --force

remove existing destination files

-n, --no-dereference

treat destination that is a symlink to a directory as if it were a normal file

Example of hard link vs symbolic link when removing source file

Hard link:

To test the different between a hard link and a symbolic link I performed the following:

Create a hard link of a test source file:

$ cat test_source
This is the source test file
$ ln test_source test_link

Check the hard link works

$ cat test_link
This is the source test file

Remove the source file

$ rm test_source

Check if the link file still exists

$ ls -ltr
total 40
-rw-r--r-- 1 krobbe dba    29 Jul  6 10:31 test_link
$ cat test_link
This is the source test file

Result: the link file still contains the information that was in the source file even though the source file has been removed.

Symbolic link:

Create soft link:

$ ln -s test_link test_link2

Check the soft link works correctly

$ cat test_link2
This is the source test file
$ ls -ltr
-rw-r--r-- 1 krobbe dba    29 Jul  6 10:31 test_link
lrwxrwxrwx 1 krobbe dba     9 Jul  6 10:34 test_link2 -> test_link

Remove the source file

$ rm test_link

Check the status of the link

$ ls -ltr
total 36
lrwxrwxrwx 1 krobbe dba     9 Jul  6 10:34 test_link2 -> test_link
$ cat test_link
cat: test_link: No such file or directory

Result: The symbolic link is highlighted as there being an error and when we go to view the contents of the file, no file or directory found error is presented.

Linux – Run command as a background process


To run a command as a background process you must append & to the end of the command when issuing:

$ <command> &

If you wish the command to continue running even when you have logged off, you need to issue nohup at the beginning of the command:

$ nohup <command>

If you wish to run the command in the background even when you are logged off you should append with nohup and &:

$ nohup <command> &

Linux – Find processes running by user


To find which processes are running by a given user on linux you can issue the following:

$ ps -fu <username>

Linux – GZip decompress files


GZIP can compress a file reducing it to 70 or 80 percent. Unlike tar it replaces the original file with a compressed version. The compressed file has the same ownership modes, access time, and modification time as the original.

Decompress Files

To decompress files use

gzip –d, gunzip,

or

gzcat

Options

gzip filename.txt

This will produce a compressed file called filename.txt.gz, and filename.txt will be deleted.
To decompress filename.txt.gz run the following:

gunzip filename.txt.gz

This will produce filename.txt and delete filename.txt.gz

Using the -n

Sets the speed at which files are compressed. The faster a file is compressed, the lower the amount of compression performed. With the -# option, # can be any number between 1 and 9 where 1 is the fastest and 9 is the slowest. The –fast option is the equivalent to -1 whereas –best is the equivalent to -9. By default gzip performs compression as thought the -6 option was specified.

Linux – remove files older than 7 days

June 2, 2011 1 comment

The Linux find command has several different uses. One of these uses is to remove files that are older than x amount of days.

I find this particularly useful when I want to clear down trace files that have been building up over time. To do this I would move to the oracle bdump directory, then execute the following command line code:

$ find *.trc -mtime +7 -exec rm {} \;

The above code will remove all trace files in the current directory that are older than 7 days.

Code breakdown

find <filename> -mtime +<number of days> -exec <command> \;

Find <filename> – finds the files specified by the filename
-mtime +<number of days> – days older than current system date
-exec <command> – command to execute with found files

Further reading

It is possible to execute different commands upon found files, however this is outside the scope of this post. For more information on the find command please see the find man pages: Find Man Page