Posts Tagged ‘shell’

NTFS and Debian

divendres, gener 15th, 2010

Long time ago it was a pain in the neck to use ntfs units with Debian. Now we have a third generation driver in a package called ntfs-3g, but there is something extra to have a unit (specially external ones) working with write permissions. I had an external hard disk LG formatted with NTFS, and I wanted to make it work with my Debian Squeeze.

apt-get install libfuse2 fuse-utils ntfs-3g ntfsprogs

though probably with only ntfs-3g would work.

After that, plug your unit in, discover its device name (for instance, $ dmesg | tail -20), and mount it with the following line:

mount -t ntfs-3g /dev/sdb1 /media/LG -o umask=0,nls=utf8

Ready :)

shell: some useful commands

dissabte, març 28th, 2009

The Linux shell offers lots of commands, but there are a few used every day. For a standard user, the shell isn’t really important. We can do (almost) whatever using GUIs, but there are some actions that are to be done with a shell. It’s powerful, faster, and easier. How do you put in a text file all permissions of all files in a given directory? How do you modify a line matching a pattern all system wide? We shall have a look at the frequently used commands:

more, less

You can use more for paging through text one screenful at a time. There is another command, less, that does the same but faster. more came first, but while it only used to allow moving forward through the text, the less command let the user go backwards. Nowadays, we can use both commands as synonyms.

$ ls -l /etc/ | more

$ ls -l /etc/ | less

$ less file.text

$ more file.text

Warning: type q to quit less or more.

cat

It’s kind of more. It outputs the contents of a file.

$ cat text.file

sort

Another filter that sorts lines of text files. The manual is quite large, so let’s see a few quick examples:

$ sort /etc/htpasswd

Shows on stdout (the screen, by default), the /etc/htpasswd  sorted alphabetically by lines.

$ sort -t: -k3 -n /etc/passwd

Performs a numeric sort  (-n) by the 3rd field (-k3). Two fields are separated by  ‘:’ (-t:).

$ ls -l /etc | sort -k5 -n -r | less

Orders the output of ls -l /etc by  the 5th field (the file’s size, as man ls says) descending (-r). We use the field separator by default (one or more white spaces).

cut

Selects parts of lines of the input file.

$ cut -d: -f3,5 /etc/passwd

Shows the 3rd and 5th field of all lines of /etc/passwd, having ‘:‘ as a field separator (-d:). Notice that with the sort command, we used -t to choose the separator.

$ ls -l /etc/passwd | cut -c2-10

Only outputs the file permissions, as they are on columns 2 to 10 in ls -l /etc/passwd.

head

Selects the first lines or characters in a file. We can use it after a pipe ( | ) or as a usual command:

$ cat /etc/passwd/ | head -2

Outputs the 2 first lines of /etc/passwd

$ head -2 /etc/passwd

idem :)

You have learnt the cut usage, haven’t you? Then mix your knowledge:

$ sort /etc/passwd | head -2 | cut -d: -f1

Shows the 2 first user id of the system, having the file sorted alphabetically.

tail

Opposite of head. Outputs the last lines or characters of files.

$ ls -l /etc | tail -2

shows the two last lines of the ls -l /etc output. Is it useful? Not really, yet. Use sort:

$ ls -l /etc  | sort -k5 -n | tail -2

Shows the 2 biggest files in /etc

tr

This is one of the most useful commands. It just translates characters. e.g: you want to replace all ‘a’ chars in a file for ‘z’, or e.g: you wish to delete all ‘b’ in a file (replace ‘b’ with ‘BLANKSPACE’):

$ tr aeiou AEIOU < /etc/passwd

the < character means: «redirect /etc/passwd to the standard input of the tr command». This line replaces all lower-case vowels to upper-case. a->A, e->E and so on. The first letter in «aeiou» corresponds to the first letter in «AEIOU». Thus, if you want to replace a,e,i,o,u with ‘V’, do:

$ tr aeiou VVVVV < dummyfile

or

$ tr aeiou V < dummyfile

This line doesn’t modify the /etc/passwd or the dummyfile. If you wish so, you have to redirect the output to the file you want to save the changes in, like that:

$ tr aeiou VVVVV < dummyfile > dummyout.

DON’T DO IT WITH /etc/passwd

wc

As simple as «word-count», but powerful: it can count lines, words and characters of the input. Example:

$ wc -c /etc/passwd

How many characters does /etc/passwd contain?

$ ls -l /etc | wc -l

how many files and directories are in /etc ?

uniq

reports or ommits repeated lines in a list. «compacts» them. e.g: We have a file containing:

edu@debian:~$ cat dummyfile
Hi,
Hi,
This is a script of Family Guy.
Have a good day,
Have a good day,
Have a good day,

Edu

Eduard Gamonal

edu@debian:~$

Its got repeated lines. I like counting things and I wish to know how many repeated lines are there.

edu@debian:~$ cat dummyfile | uniq -c
2 Hi,
1 This is a script of Family Guy.
3 Have a good day,
1
1 Edu
1
1 Eduard Gamonal
1
edu@debian:~$

Funny, but not really useful. What if instead of a dummy file we had a list of processes running on our system?

$ ps -e | cut -c25- | sort | uniq -c

For each running command , outputs the processes that are executing it. ps -e shows the command name from the 25th column on (notice the last hyphen in -c25- ).

It may seem complex sometimes, and the user my spend half an our to type a correct command, but after that we have the output we wanted and we can use it, since it’s text in a shell, not in a window.

Shell, find it! Some examples of the find command line tool

dilluns, març 16th, 2009

Have you lost something? You may fall in love with the find command. GNU Find tells you where a file is, given a condition. You shall use regular expressions and parameters related to lots of file features, like last access date, last modification date, its i-node, the file format of the device where it’s being saved, etc.

The syntax is find dir1 … dirM cond1 .. condN. It will search the directory tree rooted at each diri, for i = 1 to M, all files matching conditions cond1 to condN evaluated from left to right.

Let’s see some examples:

  1. find /bin -links +1
    Searches files in /bin and its subdirectories having 1 or more hard links.
  2. find /bin -links +1 -type f
    Idem, but only matches regular files (-type f).
  3. find -size +8k -printf ‘%p %s\n’
    If no directory root is given, the default is . (the working one). This command seeks files whose size is at least 8KB and writes its name and size.
  4. find . -name core -exec rm -i {} \; -print
    Looks for files called «core» (-name core), asks for confirmation to delete them (-exec rm -i {} \;) and, after all, prints their name (-print). Notice that you can run any command with «-exec». In rm -i, -i stands for «Ask for a confirmation», and «{}» means «the file matching the conditions». «-exec» must be closed with «\;».
  5. find /usr/include -name ‘*.h’ -exec grep -H SIGCHLD {} \;
    Looks from /usr/include on all files with the extension .h (-name ‘*.h’, with quotes to avoid the shell understanding the * as a metacharacter and letting the find command use it) and having the string «SIGCHLD». That is, for each file that has matched the conditions, «grep -H SIGCHLD» is run.
  6. find -atime +1 -type f -exec mv {} TMP \;
    Moves files that haven’t been accessed today (-atime +1) to the dir called TMP, in our working directory.

Thanks FIB.


Entra