Personal tools




Linux commands

From OrganicDesign Wiki

Jump to: navigation, search
This is the start of a list of useful Linux shell commands (generally "one liners") we use a lot


Contents

1 Ubuntu package management

1.1 Searching for an installed package

Use dpkg and grep;

dpkg -l | grep java

will list all packages with java in the name

1.2 Searching for installable packages

apt-cache performs a variety of operations on APT“s package cache

apt-cache search java

lists all packages in the cache with java in the name that are installable

2 Files

2.1 Get the size of a directory and its contents

du -sh /home/foo

2.2 Search for file content recursively

Here is an example looking for library(.+) or require(.+) within R files recursively


## Printing all matches
find . -name "*.R" -print | xargs perl -ne 'm/(library|require)\(\"?(.+?)\"?\)/ && print "$2\n"'
## Building a hash table of unique matches
find . -name "*.R" -print | xargs perl -ne 'm/(library|require)\(\"?(\w+?)\"?\)/ && $x{$2}++; END{print "@{[keys(%x)]}\n"}'

If you have spaces or newlines in file names, then you have to use -print0 option instead of -print and xargs -null so that the list of file names are exchanged with null-terminated strings.


## Printing all matches
find . -name "*.R" -print0 | xargs -0 perl -ne 'm/(library|require)\(\"?(.+?)\"?\)/ && print "$2\n"'
## Building a hash table of unique matches
find . -name "*.R" -print0 | xargs -0 perl -ne 'm/(library|require)\(\"?(\w+?)\"?\)/ && $x{$2}++; END{print "@{[keys(%x)]}\n"}'

There are other tips at stackoverflow.com.

2.3 Search for file content recursively and tar

## Find and tar
find . -name "*.R" -print0 | xargs -0 tar -cvf Rfiles.tar
## check contents
tar -tvf Rfiles.tar
 
## Find and tar.gz
find . -name "*.R" -print0 | xargs -0 tar -zcvf Rfiles.tar.gz
## check contents
tar -ztvf Rfiles.tar.gz

2.4 Search for file content recursively and long list

## Printing all matches
find . -name "*.R" -ls

2.5 Search and replace content in files

You could also use find and sed, but I find that this little line of perl works nicely.
perl -pi -w -e 's/SEARCH/REPLACE/g;' *.php
  • -e means execute the following line of code.
  • -i means edit in-place
  • -w write warnings
  • -p loop

EXTS="7z t7z"

3 Image Manipulation

3.1 Resizing JPG's and changing quality setting

The first line shows how to reduce and image to 25% and quality to 50% adding "_resized" to the results filename. The second command uses Perl to apply this same command to all JPG's in the current directory.

convert foo.jpg -resize 25% -quality 50% foo_resized.jpg
 
perl -e 'for (glob "*.jpg") { $img = $_; s/(....)$/_resized$1/; qx "convert $img -resize 25% -quality 50% $_"; }'

3.2 Apply an opaque background of a specified colour to a directory of transparent PNG's

  • This command requires ImageMagick to be installed
  • It loops through all PNG's in the CWD and puts them in a directory called processed which must exist
perl -e 'qx "convert $_ -background #ff00ff -flatten foo/$_" for glob "*.png"'

3.3 See also

4 Network commands

4.1 Restart the network after changing configuration

/etc/init.d/networking restart

4.2 List all the listening sockets and their ports and programs

netstat -lp

4.3 Get current default gateway

netstat -nr

The default gateway is on the last line, it should have the U and G flags set

4.4 Release DHCP lease

dhclient -r

4.5 Obtain a new DHCP lease

dhclient

5 Devices

5.1 mount

All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the file system found on some device to the big file tree.

mount [/dev/device] [/media/directory]

5.2 fstab

Static information about the filesystems (fstab) is a configuration file that contains information of all the partitions and storage devices in your computer. It is a map of devices to the point in the filesytem where the device can be accessed, it contains information of where your partitions and storage devices should be mounted and how. It acts as a set of defaults for devices that are specified using the mount command where the file system directory is not specified. See W:Fstab#Example.


cat /etc/fstab

5.3 Mount a .iso

See this HOWTO

6 Port forwarding

Port forwarding allows a remote client to gain access to a network so an intranet can be accessed.

ssh -fN -L[PORT]:appserver:[PORT] username@sshdserver

Then point your webbrowsers proxy server to appserver:[PORT] and access the intranet etc.

To subvert a firewalled environment where outgoing ssh is allowed.

$ ssh -D 9000 username@remotehost

Then point your web browser to a SOCKS proxy @ localhost:9000

Further it's possible to get ssh through a web proxy using corkscrew.

ssh -oProxyCommand='corkscrew local_web_proxy proxy_port %h %p' username@remotehost

7 See also

The GNU Project Debian Linux Ubuntu Linux Wikipedia Affiliate Button MediaWiki