Change: Linux coding

created on Sept. 15, 2012, 4:23 p.m. by Hevok & updated on Oct. 15, 2012, 10 p.m. by Hevok

Welcome to the wonderful UNIX world. Here we are starting to learn your command-fu.

Why to use the command line? Because there is no better way to process multiple files, or multiple computers or multiple commands on multiple computer.

It is the way for massive file manipulation.

Installing

To install packages/software use either apt-get or better aptitude (which is newer): ::

sudo apt-get install <package_name> # The classic standard way
sudo apt-get remove <package_name>
sudo apt-get install aptitude # Installs aptitude as an example.
sudo aptitude install <package_name> # you can use aptitude just as apt-get.

We can search for an available package like this: ::

sudo apt-cache search <package_name>

Multiple packages can be commanded to be installed in a single statement: ::

sudo apt-get install python-virtualenv python-django python-django-south python-numpy python-scipy

Updating

Individual package can be be updated/upgraded or everything on your system at once with this commands: ::

sudo aptitude udpate
sudo aptitude upgrade

Packaging

tar enables packaging: ::

tar czf new-tar-file-name.tar.gz file-or-folder-to-archive
  • tar - tar command
  • c - create new archive
  • z - compress the archive using gzip
  • f - use archive file.

It can be memorized with the phrase: "TARget Create Zip File

Extracting

To extract a package use for instance gunzip like this [http://www.cyberciti.biz/faq/howto-compress-expand-gz-files/]: ::

gunzip file.gz
gunzip < file.tar.gz | tar xvf-

Alternatively tar can be used: ::

tar xzf file.tar.gz
tar xzvf file.tar.gz # Shows the files being extracted during unpacking.

zip archived files are unziped like this: ::

unzip file.zip -d destination_folder

Editing

There are various light-weight editor build-in in many systems. The AND operator runs a application in the background [http://www.hashbangcode.com/blog/running-commands-background-linux-479.html].

nano file.txt
gedit file.txt
gedit file.txt & # Asynchronous opening (allows to continue work in the terminal in parallel while gedit runs in the background).
cp file.txt file.txt.bak% # Runs a file copy in the background

Searching

Unix provides a number of powerful built-in search capabilities. Among them are find, locate and grep.

find

Finding files in Unix is very simple: ::

find # Searches for files on system.
find . -name "*.txt" -mtime 5 # Find all files with .txt in the name that were modified in the last 5 days.

find / -type -mmin -10 # -f means normal files.
find ~ -iname "*xxx*" -exec mv -v {} /media/pr0n/ \ #~ home iname ' case insensitive #Moves all found files to media/pr0n [http://www.youtube.com/watch?v=x_R_JSiupzo&]feature=fvwrel]

locate

Locating files is extremely efficient as it uses indeces: ::

sudo updatedb # udpates the locate command.
locate <filename>
locate -i <filename> # -i makes the search non case-sensitive.

grep

grep allows to find (grab) a text string in files: ::

grep # Quickly finds text within files (even searching through subdirectories).
grep -ir "text string" * # Searches through all files in the current directory and below it for "text string".

Using

To display the available memory run: ::

cat /proc/meminfo

To see the memory allocation and to free up unused resources run: ::

dmesg | grep memory

The df command allows to check out the disk filesystem usage: ::

df

Freeing up the cache:

The cache can be empties with a one liner [http://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache]: ::

sync && echo 3 | sudo tee /proc/sys/vm/drop/caches

Killing

At the terminal a single running processes can be killed with Ctrl+C.

A specified number of processes are killable by applying: ::

ps aux | grep [process_name]
kill [number_of_processes]

All processes running under a name (e.g. python) can be easily killed, all at once with killall [process_name].

Shutdown

The system can be shutdown either by giving a countdown or right now: ::

sudo shutdown -P now # replace now by a countdown time in seconds if desired.

Tags: tutorial, coding
Categories:
Parent: Tutorials

Comment: Added unzipping.

See entry | Admin

Comment on This Data Unit