GGshow reloaded GGshow reloaded

April 11, 2016

RPi RF Transmitter & Radio Controlled Sockets

Playing with my new toy – Pi-mote Control starter kit with 2 sockets.

Sample code – turn on all plugs for 5 seconds.

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(17,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)

GPIO.output(17,True)
GPIO.output(22,True)
GPIO.output(23,False)
GPIO.output(27,True)
sleep(0.1)
GPIO.output(25,True)
sleep(0.25)
GPIO.output(25,False)

sleep(5)

GPIO.output(17,True)
GPIO.output(22,True)
GPIO.output(23,False)
GPIO.output(27,False)
sleep(0.1)
GPIO.output(25,True)
sleep(0.25)
GPIO.output(25,False)

Complicated? There is an easier way, by using Energenie library.

Installing Energenie library

For Python 3

sudo apt-get install python3-pip
sudo pip-3.2 install energenie

For Python 2

sudo apt-get install python-pip
sudo pip install energenie

* pip is a package management system used to install and manage software packages written in Python.

Sample code – turn on all plugs for 5 seconds

from energenie import switch_on, switch_off
from time import sleep
switch_on()
sleep(5)
switch_off()

It is much easier now, doesn’t it?

Updated: 11/9/2016
To control each switch separately, hold down the green button on one switch for 10-15 seconds, send channel 1 on command, hold down the green button on the other switch for 10-15 seconds, send channel 2 on command.
Now I can switch on/off the plug separately.

from energenie import switch_on, switch_off
from time import sleep
switch_on(1)
sleep(3);
switch_off(1)
sleep(3);
switch_on(2)
sleep(3);
switch_off(2)

Now I’ll be able to automatically turn on my radio every morning to wake me up, or remotely turn on my rice cooker before I arrive at home 😉

Reference
Controlling electrical sockets with Energenie Pi-mote – Raspberry Pi
Energenie Documentation
Energenie Python library source code

March 31, 2016

RPi 5 inch HDMI LCD

Bought a 5″ HDMI LCD screen for my Raspberry Pi, but it wasn’t working until I made some changes at /boot/config.txt file to force 800×480 screen resolution, edit /boot/config.txt and modify these lines:

hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0

Raspberry Pi 5inch HDMI LCD

 

Optionally to enable support of MJPEG, VP6, VP8, Ogg Theora & Ogg Vorbis, add these lines:

start_file=start_x.elf
fixup_file=fixup_x.elf
Filed under: Internet of Things (IoT),Linux,Raspberry Pi — Tags: , , , , — GG @ 2:08 pm

February 27, 2016

RPi camera

Enabling camera on your RPi

  • sudo raspi-config
    

Terminal commands

  • To take a picture
    raspistill -o filename.jpg
    
  • To play a recorded h264 video
    omxplayer filename.h264
    
  • To record a slow motion video (10 seconds, resolution 640×480 pixels, 90 frames per second)
    raspivid -w 640 -h 480 -fps 90 -t 10000 -o filename.h264

Installing Python API for RPi camera

  • sudo apt-get install python-picamera python3-picamera python-rpi.gpio
    

Python code samples

  • Preview and capture a photo
    import time
    import picamera
    with picamera.PiCamera() as camera:
        camera.start_preview()
        time.sleep(10)
        camera.capture('filename.jpg')
        camera.stop_preview()
    
  • Capturing multiple photos
    import time
    import picamera
    with picamera.PiCamera() as camera:
        i = 0
        while True:
            camera.start_preview()
            time.sleep(1)
            camera.capture(str(i)+(".jpg"))
            i += 1
            time.sleep(5)
    
  • Capturing photo when push switch pressed
    * connect a push switch to GPIO2 & GND
    * use current timestamp as filename
    * save JPG when push switch pressed

    from time import gmtime, strftime, sleep
    import picamera
    import RPI.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)
    button=2
    GPIO.setup(button,GPIO.IN,pull_up_down=PIO.PUD_UP)
    while True:
        with picamera.PiCamera() as camera:
            camera.resolution = 1920,1080
            GPIO.wait_for_edge(button, GPIO.FALLING)
            camera.start_preview()
            sleep(2)
            camera.capture(strftime("%Y%m%d%H%M%S",gmtime())+'.jpg')
            camera.stop_preview()
    

    A photo posted by @le_g_end on

    #raspberrypi #camera & #pushswitch

    A video posted by @le_g_end on

  • Recording a 10 seconds video
    from time import sleep
    import picamera
    with picamera.PiCamera() as camera:
        camera.start_recording('filename.h264')
        sleep(10)
        camera.stop_recording()
    
    
  • Video recording and playback
    import os
    while True:
        os.system("raspivid -w 640 -h 480 -fps 90 -t 10000 -o filename.h264")
        os.system("omxplayer filename.h264")
    

Reference

RPi Chapter 1

Preparation

  1. Download Raspbian and install it on SD card.
  2. Login using default username: pi, and password: raspberry
  3. Run Xserver
    startx
    
  4. Update and cleanup
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get clean
    
  5. Installing packages
    sudo apt-get install {package}
    

March 9, 2015

How to block a specific network port in Linux?

Question:

How to block a specific network port in Linux?

Answer:

Use iptables commands.
E.g. to block port 21 & 22:

iptables -A OUTPUT -p tcp --dport 21 -j REJECT
iptables -A OUTPUT -p tcp --dport 22 -j REJECT

To block a port permanently

  1. save iptables to a file,
    iptables-save > /etc/sysconfig/iptables
  2. add this line to /etc/rc.local
    iptables-restore < /etc/sysconfig/iptables
Filed under: Linux,Web Servers — Tags: , , — GG @ 5:27 pm

February 10, 2013

Interesting route of 216.81.59.173

Try run this command in your terminal:

traceroute -m 100 216.81.59.173

Or if you are using Windows:

tracert -h 100  216.81.59.173

It gives output like this:

...
 17     *        *        *     Request timed out.
 18   595 ms   619 ms   608 ms  Episode.IV [206.214.251.1]
 19   566 ms   605 ms   703 ms  A.NEW.HOPE [206.214.251.6]
 20   580 ms   658 ms   629 ms  It.is.a.period.of.civil.war [206.214.251.9]
 21   587 ms   648 ms   658 ms  Rebel.spaceships [206.214.251.14]
 22   564 ms   629 ms   619 ms  striking.from.a.hidden.base [206.214.251.17]
 23   565 ms   658 ms   610 ms  have.won.their.first.victory [206.214.251.22]
 24   611 ms   622 ms   619 ms  against.the.evil.Galactic.Empire [206.214.251.25]
 25   564 ms   600 ms   618 ms  During.the.battle [206.214.251.30]
 26   643 ms   641 ms   624 ms  Rebel.spies.managed [206.214.251.33]
 27   575 ms   638 ms   598 ms  to.steal.secret.plans [206.214.251.38]
 28   614 ms   645 ms   612 ms  to.the.Empires.ultimate.weapon [206.214.251.41]
 29   639 ms   688 ms   702 ms  the.DEATH.STAR [206.214.251.46]
 30   596 ms   639 ms   629 ms  an.armored.space.station [206.214.251.49]
 31   562 ms  1452 ms   378 ms  with.enough.power.to [206.214.251.54]
 32   405 ms   389 ms   409 ms  destroy.an.entire.planet [206.214.251.57]
 33   426 ms   438 ms   438 ms  Pursued.by.the.Empires [206.214.251.62]
 34   442 ms   441 ms   449 ms  sinister.agents [206.214.251.65]
 35   436 ms   559 ms   439 ms  Princess.Leia.races.home [206.214.251.70]
 36   417 ms   448 ms   441 ms  aboard.her.starship [206.214.251.73]
 37   430 ms   438 ms   438 ms  custodian.of.the.stolen.plans [206.214.251.78]
 38   452 ms   450 ms   449 ms  that.can.save.her [206.214.251.81]
 39   463 ms   430 ms   427 ms  people.and.restore [206.214.251.86]
 40   444 ms   469 ms   439 ms  freedom.to.the.galaxy [206.214.251.89]
 41   535 ms   438 ms   449 ms  0-------------------0 [206.214.251.94]
 42   437 ms   498 ms   439 ms  0------------------0 [206.214.251.97]
 43   426 ms   438 ms   449 ms  0-----------------0 [206.214.251.102]
 44   436 ms   439 ms   478 ms  0----------------0 [206.214.251.105]
 45   444 ms   438 ms   449 ms  0---------------0 [206.214.251.110]
 46  2227 ms   387 ms   388 ms  0--------------0 [206.214.251.113]
 47   392 ms   581 ms   399 ms  0-------------0 [206.214.251.118]
 48   406 ms   399 ms   398 ms  0------------0 [206.214.251.121]
 49   416 ms   448 ms   449 ms  0-----------0 [206.214.251.126]
 50   416 ms   438 ms   438 ms  0----------0 [206.214.251.129]
 51   435 ms   438 ms   438 ms  0---------0 [206.214.251.134]
 52   445 ms   458 ms   449 ms  0--------0 [206.214.251.137]
 53   437 ms   438 ms   435 ms  0-------0 [206.214.251.142]
 54   612 ms   448 ms   448 ms  0------0 [206.214.251.145]
 55   443 ms   480 ms   448 ms  0-----0 [206.214.251.150]
 56   455 ms   469 ms   494 ms  0----0 [206.214.251.153]
 57   402 ms   398 ms   398 ms  0---0 [206.214.251.158]
 58   415 ms   399 ms   388 ms  0--0 [206.214.251.161]
 59   428 ms   448 ms   433 ms  0-0 [206.214.251.166]
 60   458 ms   440 ms   439 ms  00 [206.214.251.169]
 61   436 ms   458 ms   459 ms  I [206.214.251.174]
 62   429 ms   449 ms   449 ms  By.Ryan.Werber [206.214.251.177]
 63   437 ms   458 ms   447 ms  When.CCIEs.Get.Bored [206.214.251.182]
 64   475 ms   456 ms   471 ms  read.more.at.beaglenetworks.net [206.214.251.185]
 65   437 ms   438 ms   438 ms  FIN [216.81.59.173]

Trace complete.
Filed under: Linux,Windows — Tags: , , , , , , — GG @ 6:37 pm

December 27, 2012

Linux command – 1.6 – File size & disk space

df – report file system disk space usage

Show information about the file system on which each FILE resides, or all file systems by default.

Usage:

df [OPTION]... [FILE]...

Options:

-a, --all include dummy file systems
-B, --block-size=SIZE use SIZE-byte blocks
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, --si likewise, but use powers of 1000 not 1024
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
-P, --portability use the POSIX output format
 --sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Example: display available disk space

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vzfs 30G 21G 9.3G 70% /
none 512M 4.0K 512M 1% /dev
/dev/vzfs 30G 21G 9.3G 70% /var/named/run-root/var/run/dbus

du – estimate file space usage

Summarize disk usage of each FILE, recursively for directories.

Usage:

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

Options:

-a, --all
write counts for all files, not just directories

--apparent-size
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in (`sparse') files, internal fragmentation, indirect blocks, and the like

-B, --block-size=SIZE
use SIZE-byte blocks

-b, --bytes
equivalent to `--apparent-size --block-size=1'
-c, --total
produce a grand total

-D, --dereference-args
dereference FILEs that are symbolic links

--files0-from=F
summarize disk usage of the NUL-terminated file names specified in file F

-H
like --si, but also evokes a warning; will soon change to be equivalent to --dereference-args (-D)

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)

--si
like -h, but use powers of 1000 not 1024

-k
like --block-size=1K

-l, --count-links
count sizes many times if hard linked

-m
like --block-size=1M

-L, --dereference
dereference all symbolic links

-P, --no-dereference
don't follow any symbolic links (this is the default)

-0, --null
end each output line with 0 byte rather than newline

-S, --separate-dirs
do not include size of subdirectories

-s, --summarize
display only a total for each argument

-x, --one-file-system
skip directories on different file systems

-X FILE, --exclude-from=FILE
Exclude files that match any pattern in FILE.

--exclude=PATTERN
Exclude files that match PATTERN.

--max-depth=N
print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize

--time
show time of the last modification of any file in the directory, or any of its subdirectories

--time=WORD
show time as WORD instead of modification time: atime, access, use, ctime or status

--time-style=STYLE
show times using style STYLE: full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like `date'

--help
display this help and exit

--version
output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Examples: display size of a directory

$ du -hs /path/to/dir
9.2G	/path/to/dir

stat – display file or file system status

Usage:

stat [OPTION]... FILE...

Options:

-L, --dereference
follow links

-f, --file-system
display file system status instead of file status

-c --format=FORMAT
use the specified FORMAT instead of the default; output a newâ line after each use of FORMAT

--printf=FORMAT
like --format, but interpret backslash escapes, and do not outâ put a mandatory trailing newline. If you want a newline, include n in FORMAT

-t, --terse
print the information in terse form

--help
display this help and exit

--version
output version information and exit

The valid format sequences for files (without --file-system):
%a Access rights in octal
%A Access rights in human readable form
%b Number of blocks allocated (see %B)
%B The size in bytes of each block reported by %b
%C SELinux security context string
%d Device number in decimal
%D Device number in hex
%f Raw mode in hex
%F File type
%G Group name of owner
%h Number of hard links
%i Inode number
%m Mount point
%n File name
%N Quoted file name with dereference if symbolic link
%o I/O block size
%s Total size, in bytes
%t Major device type in hex
%T Minor device type in hex
%u User ID of owner
%U User name of owner
%w Time of file birth, human-readable; - if unknown
%W Time of file birth, seconds since Epoch; 0 if unknown
%x Time of last access, human-readable
%X Time of last access, seconds since Epoch
%y Time of last modification, human-readable
%Y Time of last modification, seconds since Epoch
%z Time of last change, human-readable
%Z Time of last change, seconds since Epoch

Valid format sequences for file systems:
%a Free blocks available to non-superuser
%b Total data blocks in file system
%c Total file nodes in file system
%d Free file nodes in file system
%f Free blocks in file system
%i File System ID in hex
%l Maximum length of filenames
%n File name
%s Block size (for faster transfers)
%S Fundamental block size (for block counts)
%t Type in hex
%T Type in human readable form

Example: display file system status

$ stat -f /
  File: "/"
    ID: 2b6896ea6511332f Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2090580    Free: 1663166    Available: 1558309
Inodes: Total: 524288     Free: 421462

Alternative way to display file size using ls command

$ ls -lah filename | awk '{ print $5}'
Filed under: Linux — Tags: , , , , — GG @ 8:53 pm

December 19, 2012

Linux command – 1.2 – Searching for files

find – search for files in a directory hierarchy

Usage:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

Options:

-P
Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.

-L
Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is broken). Using -L causes the -lname and -ilname predicates always to return false.

-H
Do not follow symbolic links, except while processing the command line arguments. When find examines or prints information about files, the information used shall be taken from the properties of the symbolic link itself. The only exception to this behaviour is when a file specified on the command line is a symbolic link, and the link can be resolved. For that situation, the information used is taken from whatever the link points to (that is, the link is followed). The information about the link itself is used as a fallback if the file pointed to by the symbolic link cannot be examined. If -H is in effect and one of the paths specified on the command line is a symbolic link to a directory, the contents of that directory will be examined (though of course -maxdepth 0 would prevent this).

locate – find files by name

Usage:

locate [OPTION]... PATTERN...

Options:

-b, --basename
Match only the base name against the specified patterns. This is the opposite of --wholename.-c, --count
Instead of writing file names on standard output, write the number of matching entries only.

-d, --database DBPATH
Replace the default database with DBPATH. DBPATH is a :-separated list of database file names. If more than one --database option is specified, the resulting path is a concatenation of the separate paths. An empty database file name is replaced by the default database. A database file name - refers to the standard input. Note that a database can be read from the standard input only once.

-e, --existing
Print only entries that refer to files existing at the time locate is run.

-L, --follow
When checking whether files exist (if the --existing option is specified), follow trailing symbolic links. This causes broken symbolic links to be omitted from the output. This is the default behavior. The opposite can be specified using --nofollow.

-h, --help
Write a summary of the available options to standard output and exit successfully.

-i, --ignore-case
Ignore case distinctions when matching patterns.

-l, --limit, -n LIMIT
Exit successfully after finding LIMIT entries. If the --count option is specified, the resulting count is also limited to LIMIT.

-m, --mmap
Ignored, for compatibility with BSD and GNU locate.

-P, --nofollow, -H
When checking whether files exist (if the --existing option is specified), do not follow trailing symbolic links. This causes broken symbolic links to be reported like other files. This is the opposite of --follow.

-0, --null
Separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line. This option is designed for interoperability with the --null option of GNU
xargs(1).

-S, --statistics
Write statistics about each read database to standard output instead of searching for files and exit successfully.

-q, --quiet
Write no messages about errors encountered while reading and processing databases.

-r, --regexp REGEXP
Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.

--regex
Interpret all PATTERNs as extended regexps.

-s, --stdio
Ignored, for compatibility with BSD and GNU locate.

-V, --version
Write information about the version and licence of locate on standard output and exit successfully.

-w, --wholename
Match only the whole path name against the specified patterns. This is the default behavior. The opposite can be specified using --basename.

Filter contents using ls & grep

Example: list all mp3 files in current working directory

$ ls -B |grep ".mp3"
?????.mp3
?????.mp3
?????.mp3
Filed under: Linux — Tags: , , , — GG @ 5:41 pm

December 1, 2012

Linux command – 1.3 – Viewing file

more – file perusal filter for crt viewing

View a file in terminal with ability to scroll down.

Usage:

more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...]

Options:

-num
This option specifies an integer number which is the screen size (in lines).

-d
more will prompt the user with the message "[Press space to continue, 'q' to quit.]" and will display "[Press 'h' for instructions.]" instead of ringing the bell when an illegal key is pressed.

-l
more usually treats ^L (form feed) as a special character, and will pause after any line that contains a form feed. The -l option will prevent this behavior.

-f
Causes more to count logical, rather than screen lines (i.e., long lines are not folded).

-p
Do not scroll. Instead, clear the whole screen and then display the text. Notice that this option is switched on automatically if the executable is named page.

-c
Do not scroll. Instead, paint each screen from the top, clearing the remainder of each line as it is displayed.

-s
Squeeze multiple blank lines into one.

-u
Suppress underlining.

+/
The +/ option specifies a string that will be searched for before each file is displayed.

+num
Start at line number.

less – opposite of more

View a file in terminal with ability to scroll up and down.

Usage:

less -?
less --help
less -V
less --version
less [-[+]aABcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~] [-b space] [-h lines] [-j line] [-k keyfile] [-{oO} logfile] [-p pattern] [-P prompt] [-t tag] [-T tagsfile] [-x tab,...] [-y lines] [-[z] lines] [-# shift] [+[+]cmd] [--] [filename]...

Options:

-?, --help
This option displays a summary of the commands accepted by  less (the  same  as  the  h  command). (Depending on how your shell interprets the question mark, it may be necessary to  quote  the question mark, thus: "-?".)

-a, --search-skip-screen
Forward search skips current screen.

-A, --SEARCH-SKIP-SCREEN
Forward search always skips target line.

-b[N], --buffers=[N]
Number of buffers.

-B, --auto-buffers
Don't automatically allocate buffers for pipes.

-c, --clear-screen
Repaint by clearing rather than scrolling.

-d, --dumb
Dumb terminal.

-D[xn.n], --color=xn.n
Set screen colors. (MS-DOS only)

-e, -E, --quit-at-eof, --QUIT-AT-EOF
Quit at end of file.

-f, --force
Force open non-regular files.

-F, --quit-if-one-screen
Quit if entire file fits on first screen.

-g, --hilite-search
Highlight only last match for searches.

-G, --HILITE-SEARCH
Don't highlight any matches for searches.

-h [N], --max-back-scroll=[N]
Backward scroll limit.

-i, --ignore-case
Ignore case in searches that do not contain uppercase.

-I, --IGNORE-CASE
Ignore case in all searches.

-j [N], --jump-target=[N]
Screen position of target lines.

-J, --status-column
Display a status column at left edge of screen.

-k [file], --lesskey-file=[file]
Use a lesskey file.

-K, --quit-on-intr
Exit less in response to ctrl-C.

-L, --no-lessopen
Ignore the LESSOPEN environment variable.

-m, -M, --long-prompt, --LONG-PROMPT
Set prompt style.

-n, -N, --line-numbers, --LINE-NUMBERS
Don't use line numbers.

-o [file], --log-file=[file]
Copy to log file (standard input only).

-O [file], --LOG-FILE=[file]
Copy to log file (unconditionally overwrite).

-p [pattern], --pattern=[pattern]
Start at pattern (from command line).

-P [prompt], --prompt=[prompt]
Define new prompt.

-q, -Q, --quiet, --QUIET, --silent, --SILENT
Quiet the terminal bell.

-r, -R, --raw-control-chars, --RAW-CONTROL-CHARS
Output "raw" control characters.

-s, --squeeze-blank-lines
Squeeze multiple blank lines.

-S, --chop-long-lines
Chop long lines.

-t [tag], --tag=[tag]
Find a tag.

-T [tagsfile], --tag-file=[tagsfile]
Use an alternate tags file.

-u, -U, --underline-special, --UNDERLINE-SPECIAL
Change handling of backspaces.

-V, --version
Display the version number of "less".

-w, --hilite-unread
Highlight first new line after forward-screen.

-W, --HILITE-UNREAD
Highlight first new line after any forward movement.

-x [N[,...]], --tabs=[N[,...]]
Set tab stops.

-X, --no-init
Don't use termcap init/deinit strings.
 --no-keypad
 Don't use termcap keypad init/deinit strings.

-y[N], --max-forw-scroll=[N]
Forward scroll limit.

-z[N], --window=[N]
Set size of window.

-" [c[c]], --quotes=[c[c]]
Set shell quote characters.

-~, --tilde
Don't display tildes after end of file.

-# [N], --shift=[N]
Horizontal scroll amount (0 = one half screen width)

--no-keypad
Don't send keypad init/deinit sequence.

--follow-name
The F command changes files if the input file is renamed.

head – output the first part of files

Usage:

head [OPTION]… [FILE]…Options:

-c, --bytes=[-]K
print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file

-n, --lines=[-]K
print the first K lines instead of the first 10; with the leading `-', print all but the last K lines of each file

-q, --quiet, --silent
never print headers giving file names

-v, --verbose
always print headers giving file names

--help
display this help and exit

--version
output version information and exit

K may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

tail – output the last part of files

Usage:

tail [OPTION]... [FILE]...

Options:

-c, --bytes=K
output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file

-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent

-F
same as --follow=name --retry

-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth

--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not changed size after N (default 5) iterations to see if it has been unlinked or renamed (this is the usual case of rotated log files). With inotify, this option is rarely useful.

--pid=PID
with -f, terminate after process ID, PID dies

-q, --quiet, --silent
never output headers giving file names

--retry
keep trying to open a file even when it is or becomes inaccessible; useful when following by name, i.e., with --follow=name

-s, --sleep-interval=N
with -f, sleep for approximately N seconds (default 1.0) between iterations. With inotify and --pid=P, check process P at least once every N seconds.

-v, --verbose
always output headers giving file names

pg – browse pagewise through text files

Usage:

pg [-number] [-p string] [-cefnrs] [+line] [+/pattern/] [file...]

Options:

-number
The number of lines per page. Usually, this is the number of CRT lines minus one.

-c
Clear the screen before a page is displayed, if the terminfo entry for the terminal provides this capability.

-e
Do not pause and display (EOF) at the end of a file.

-f
Do not split long lines.

-n
Without this option, commands must be terminated by a newline character. With this option, pg advances once a command letter is entered.

-p string
Instead of the normal prompt :, string is displayed. If string contains %d, its first occurrence is replaced by the number of the current page.

-r
Disallow the shell escape.

-s
Print messages in standout mode, if the terminfo entry for the terminal provides this capability.

+number
Start at the given line.

+/pattern/
Start at the line containing the Basic Regular Expression patâ tern given.
Filed under: Linux — Tags: , , , , , , — GG @ 3:55 pm

Linux command – 1.4 – File operation

cat – concatenate file(s) or standard input to standard output.

Usage:

cat [option] [file]

Options:

-A, --show-all
equivalent to -vET-b, --number-nonblank
number nonempty output lines, overrides -n

-e
equivalent to -vE

-E, --show-ends
display $ at end of each line

-n, --number
number all output lines

-s, --squeeze-blank
suppress repeated empty output lines

-T, --show-tabs
display TAB characters as ^I

-u
(ignored)

-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB

Example: Combine 2 files

$ cat file1 file2 > newfile

Example: Append a file to another file

$ cat file1 >> file2

cp – copy / duplicate files and/or directories

Usage:

cp [option] [source] [destination]

Options:

-a, --archive
same as -dR --preserve=all--attributes-only
don't copy the file data, just the attributes

--backup[=CONTROL]
make a backup of each existing destination file

-b like --backup but does not accept an argument

--copy-contents
copy contents of special files when recursive

-d same as --no-dereference --preserve=links

-f, --force
if an existing destination file cannot be opened, remove it and try again (redundant if the -n option is used)

-i, --interactive
prompt before overwrite (overrides a previous -n option)

-H follow command-line symbolic links in SOURCE

-l, --link
hard link files instead of copying

-L, --dereference
always follow symbolic links in SOURCE

-n, --no-clobber
do not overwrite an existing file (overrides a previous -i option)

-P, --no-dereference
never follow symbolic links in SOURCE

-p same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all

-c same as --preserve=context

--no-preserve=ATTR_LIST
don't preserve the specified attributes

--parents
use full source file name under DIRECTORY

-R, -r, --recursive
copy directories recursively

--reflink[=WHEN]
control clone/CoW copies. See below

--remove-destination
remove each existing destination file before attempting to open it (contrast with --force)

--sparse=WHEN
control creation of sparse files. See below

--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument

-s, --symbolic-link
make symbolic links instead of copying

-S, --suffix=SUFFIX
override the usual backup suffix

-t, --target-directory=DIRECTORY
copy all SOURCE arguments into DIRECTORY

-T, --no-target-directory
treat DEST as a normal file

-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing

-v, --verbose
explain what is being done

-x, --one-file-system
stay on this file system

-Z, --context=CONTEXT
set security context of copy to CONTEXT

Example: Copy file1 to file2

$ cp file1 file2

Example: Create a new symbolic link of file1 as file2

$ cp -s file1 file2

mv – move / rename files and/or directories

Usage:

mv [option] [source] [destination]

Options:

--backup[=CONTROL]
make a backup of each existing destination file-b
like --backup but does not accept an argument

-f, --force
do not prompt before overwriting

-i, --interactive
prompt before overwrite

-n, --no-clobber
do not overwrite an existing file

--strip-trailing-slashes
remove any trailing slashes from each SOURCE argument

-S, --suffix=SUFFIX
override the usual backup suffix

-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY

-T, --no-target-directory
treat DEST as a normal file

-u, --update
move only when the SOURCE file is newer than the destination file or when the destination file is missing

-v, --verbose
explain what is being done

Example: Rename file1 to file2

$ mv file1 file2

Example: Move folder to a new destination

$ mv /path/to/source /path/to/destination

mkdir – make a new directory

Usage:

mkdir [option] [dir]

Options:

-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask-p, --parents
no error if existing, make parent directories as needed

-v, --verbose
print a message for each created directory

-Z, --context=CTX
set the SELinux security context of each created directory to CTX

Example: Making a new directory and print a message explaining what is being done

$ mkdir -v NewFolder
mkdir: created directory `NewFolder'

rm – remove / delete a file

Usage:

rm [option] [file]

Options:

-f, --force
ignore nonexistent files, never prompt-i
prompt before every removal

-I
prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes

--interactive[=WHEN]
prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always

--one-file-system
when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument

--no-preserve-root
do not treat `/' specially

--preserve-root
do not remove `/' (default)

-r, -R, --recursive
remove directories and their contents recursively

-v, --verbose
explain what is being done

Example: Remove a directory and print a message explaining what is being done

user@ubuntu:~$ rm -rv test2
removed directory: `test2'

ln – create symbolic link

Usage:

ln [source] [destination](create a link to TARGET with the name LINK_NAME)
ln [OPTION] [-T] TARGET LINK_NAME

(create a link to TARGET in the current directory)
ln [OPTION] TARGET

(create links to each TARGET in DIRECTORY)
ln [OPTION] TARGET DIRECTORY
ln [OPTION] -t DIRECTORY TARGET

Create hard links by default, symbolic links with --symbolic. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

Options:

--backup[=CONTROL]
make a backup of each existing destination file-b
like --backup but does not accept an argument

-d, -F, --directory
allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser)

-f, --force
remove existing destination files

-i, --interactive
prompt whether to remove destinations

-L, --logical
make hard links to symbolic link references

-n, --no-dereference
treat destination that is a symlink to a directory as if it were a normal file

-P, --physical
make hard links directly to symbolic links

-s, --symbolic
make symbolic links instead of hard links

-S, --suffix=SUFFIX
override the usual backup suffix

-t, --target-directory=DIRECTORY
specify the DIRECTORY in which to create the links

-T, --no-target-directory
treat LINK_NAME as a normal file

-v, --verbose
print name of each linked file

--help
display this help and exit

--version
output version information and exit

touch – update the access time and last modified date for a file to the current time and date

Usage:

touch [option] [file]

Options:

-a
change only the access time-c, --no-create
do not create any files

-d, --date=STRING
parse STRING and use it instead of current time

-f
(ignored)

-h, --no-dereference
affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink)

-m
change only the modification time

-r, --reference=FILE
use this file's times instead of current time

-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time

--time=WORD
change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m

Example: Change the access time of a file to specified date

$ touch -t 199912131415 filename
Filed under: Linux — Tags: , , , , , , , — GG @ 3:37 pm
Older Posts »

© 2024 GGSHOW | Powered by WordPress