Showing posts with label LinuxCommandLine. Show all posts
Showing posts with label LinuxCommandLine. Show all posts

Friday, August 11, 2017

Batch renaming multiple files in Linux


[1] https://stackoverflow.com/questions/208181/how-to-rename-with-prefix-suffix

using 'rename'
rename 's/(.*)$/new.$1/' original.filename
shell script
for filename in *.jpg; do mv "$filename" "prefix_$filename"; done;
brace expansion
mv {,new.}original.filename


Friday, December 30, 2016

epoch and date conversion in Linux command line


http://stackoverflow.com/questions/14805591/how-to-convert-strings-like-19-feb-12-to-epoch-date-in-unix

date --date="19-FEB-12" +%s
Current epoch:
date +%s
So, since your dates are in the past:
NOW=`date +%s`
THEN=`date --date="19-FEB-12" +%s`

let DIFF=$NOW-$THEN
echo "The difference is: $DIFF"

Convert epoch seconds to date

Convert seconds since the epoch (1970-01-01 UTC) to a date

$ date --date='@2147483647'

Details see $man date


qyang@ubuntu:~$ date +%w
6                                                           0 is SUN, 6 is SAT
qyang@ubuntu:~$ date --date='2-JAN-2017' +%w
1                                                           1 is MON
qyang@ubuntu:~$ date --date='2-JAN-2017' +%s
1483275600


qyang@ubuntu:~$ date --date='2-JAN-2017' +%s
1483275600
qyang@ubuntu:~$ ipython
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 

In [1]: 1483275600+60*24*60*60.
Out[1]: 1488459600.0

So, in 60 days from 2-Jan-2017 will be 3-Mar-2017:
qyang@ubuntu:~$ date --date='@1488459600.0'
Fri Mar  3 00:00:00 AEDT 2017

To calculate how many days between two dates:
qyang@ubuntu:~$ date --date='31-Dec-2016' +%s
1483102800
qyang@ubuntu:~$ date --date='6-Mar-2017' +%s
1488718800
qyang@ubuntu:~$ ipython
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
Type "copyright", "credits" or "license" for more information.
In [1]: (1488718800 - 1483102800)/(24*60*60.)
Out[1]: 65.0
It's 65 days from 31-Dec-2016 to 6-Mar-2017.

Friday, November 13, 2015

Folders and files synchronisation using rsync in Linux


# Creat source folder and files and blank destination folder
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ mkdir src dst
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ mkdir src/a 
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ echo "hell a" > src/a/file1.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
└── src
    └── a
        └── file1.txt

# rsync to test folder and file synch. Both 'a' and 'file1' appeared under 'dst'.
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── a
│       └── file1.txt
└── src
    └── a
        └── file1.txt

# Test newly added folder 'b' being synched
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ mkdir src/b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   ├── a
│   │   └── file1.txt
│   └── b
└── src
    ├── a
    │   └── file1.txt
    └── b

# Test newly added file 'file2.txt' being synched
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ echo "hell b" > src/b/file2.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   ├── a
│   │   └── file1.txt
│   └── b
│       └── file2.txt
└── src
    ├── a
    │   └── file1.txt
    └── b
        └── file2.txt

# Test newly delete folder 'a' and 'file1.txt' inside being synched.
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rm -rf src/a/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    └── b
        └── file2.txt

# Test newly delete file 'file2.txt' being synched.
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rm src/b/file2.txt 
rm: remove regular file ‘src/b/file2.txt’? y
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
└── src
    └── b

# Add back 'file2.txt' and check
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ echo "hell b" > src/b/file2.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    └── b
        └── file2.txt

# modify 'file2.txt' and check synch
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cat dst/b/file2.txt
hell b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ vi src/b/file2.txt
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cat src/b/file2.txt
Modified ...hello b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ rsync --delete --checksum --recursive src/ dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cat dst/b/file2.txt
Modified ...hello b
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    └── b
        └── file2.txt


Using cp command line for multiple files copy
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   └── b
│       └── file2.txt
└── src
    ├── a
    │   └── file1.txt
    └── b
        ├── file2.txt
        └── file3.txt

5 directories, 4 files
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ cp {src/a/file1.txt,src/b/file{2..3}.txt} dst/
qyang@lubuntu-laptop:~/Trash/Sandbox-Trial$ tree
.
├── dst
│   ├── b
│   │   └── file2.txt
│   ├── file1.txt
│   ├── file2.txt
│   └── file3.txt
└── src
    ├── a
    │   └── file1.txt
    └── b
        ├── file2.txt
        └── file3.txt

Tuesday, October 27, 2015

Pattern matching and regular expressions in Linux Command Line


[1] tldp.org/LDP/GNU-Linux-Tools-Summary
[2] unix.stackexchange.com/questions/28155/find-files-with-certain-extensions

Regular expressions (regex):
PATTERN=re.compile(r'''((?:[^\t "']|"[^"]*"|'[^']*')+)''')

From [1]


[ ] (square brackets)
specifies a range. If you did m[a,o,u]m it can become: mam, mum, mom if you did: m[a-d]m it can become anything that starts and ends with m and has any character a to d inbetween. For example, these would work: mam, mbm, mcm, mdm. This kind of wildcard specifies an “or” relationship (you only need one to match).
{ } (curly brackets)
terms are separated by commas and each term must be the name of something or a wildcard. This wildcard will copy anything that matches either wildcard(s), or exact name(s) (an “or” relationship, one or the other).
For example, this would be valid:
cp {*.doc,*.pdf} ~
This will copy anything ending with .doc or .pdf to the users home directory. Note that spaces are not allowed after the commas (or anywhere else).

From [2]

find -regex ".*\.\(xls\|csv\)"


find -name "*.xls" -o -name "*.csv"

Saturday, September 19, 2015

Mount ISO file as a disk in Linux

[Ref] http://wiki.sugarlabs.org/go/Sugar_on_a_Stick/Installation


#mount /path/to/downloaded.iso /run/soas/




[root@fedora20 soas]# umount /run/media/q.yang/KINGSTON

[root@fedora20 mntUSBFlash]# /home/q.yang/run/soas/LiveOS/livecd-iso-to-disk --reset-mbr --overlay-size-mb 160 --home-size-mb 170 --unencrypted-home /home/q.yang/mntWin7Desktop/Fedora-Live-SoaS-i686-22-3.iso /dev/sdb1
Verifying image...
/home/q.yang/mntWin7Desktop/Fedora-Live-SoaS-i686-22-3.iso:   7101474df5e036dedae3ecca5bd918a1
Fragment sums: 6d845885a71ffb934a43ea879cfa8fffdbc16c495a88862419c0f9bf6a2d
Fragment count: 20
Press [Esc] to abort check.
Checking: 100.0%

The media check is complete, the result is: PASS.

It is OK to use this media.

/dev/sdb1 is mounted, please unmount for safety

Friday, August 22, 2014

Install Google chrome in Fedora20

[1]   http://www.if-not-true-then-false.com/2010/install-google-chrome-with-yum-on-fedora-red-hat-rhel/

cat << EOF > /etc/yum.repos.d/google-chrome.repo
[google-chrome]
name=google-chrome - \$basearch
baseurl=http://dl.google.com/linux/chrome/rpm/stable/\$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
EOF

#yum install google-chrome-stable

-Start chrome
$google-chrome


Install GNOME Weather in Fedora 20


[1] http://askubuntu.com/questions/302590/how-to-install-a-clock-or-weather-application-with-gnome-3-8
[2] http://fedoraftw.com/enjoy-weather-data-fedora-20-polished-gnome-weather


-Install gnome-weather with root account

# yum install gnome-weather

...........
  Installing : gnome-weather-3.10.1-1.fc20.x86_64                           1/1
  Verifying  : gnome-weather-3.10.1-1.fc20.x86_64                           1/1

Installed:
  gnome-weather.x86_64 0:3.10.1-1.fc20                                         

Complete!


-Start gnome-weather

$gnome-weather



Wednesday, June 11, 2014

Enable SSH service in Fedora20 in command line


[root@fedora20 SandBoxPython]# systemctl enable sshd.service
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'
[root@fedora20 SandBoxPython]# systemctl restart sshd.service

Thursday, June 5, 2014

Trace route command line under both windows and Linux

>tracert hostnameOrIP

$traceroute hostnameOrIp


C:\Users\QuentinYang>tracert 10.10.10.56

Tracing route to ENG-QUENTIN [10.10.10.56]
over a maximum of 30 hops:

  1    35 ms    33 ms    33 ms  JUPITER [10.10.10.66]
  2    34 ms    36 ms    39 ms  ENG-QUENTIN [10.10.10.56]

Trace complete.


[q.yang@fedora20 ~]$ traceroute 10.10.10.56
traceroute to 10.10.10.56 (10.10.10.56), 30 hops max, 60 byte packets
 1  10.10.10.66 (10.10.10.66)  47.483 ms  49.967 ms  49.689 ms
 2  10.10.10.56 (10.10.10.56)  52.431 ms  52.185 ms  54.700 ms


quentin@ubuntu:~$ traceroute 10.10.10.56
traceroute to 10.10.10.56 (10.10.10.56), 30 hops max, 60 byte packets
 1  ubuntu (10.1.1.11)  3000.618 ms !H  3000.615 ms !H  3000.547 ms !H

Thursday, November 15, 2012

Talking to 3G USB Sierra Modem Under Linux


[root@GsnCommsModule root]# cat /dev/ttyUSB4 &
[1] 601
[root@GsnCommsModule root]# cat > /dev/ttyUSB4
AT!SELRAT?



OK

AT!SELRAT=?



OK

AT!GETBAND?



OK



Sunday, February 26, 2012

How to generate patch under Linux

[1] Linux byexamples


First, how to create patch file?
------------------------------------
Patch file is a readable file that created by diff with -c (context output format). It doesn’t matter and if you wanna know more, man diff. To patch the entire folder of source codes(as usually people do)I do as bellow:

Assume Original source code at folder Tb01, and latest source code at folder Tb02. And there have multiple sub directories at Tb01 and Tb02 too.

diff -crB Tb01 Tb02 > Tb02.patch


-c context, -r recursive (multiple levels dir), -B is to ignore Blank Lines.
I put -B because blank lines is really useless for patching, sometimes I need to manually read the patch file to track the changes, without -B is really headache.

How to patch?
-------------------------
First of all, please do a dry-run before really patch it. Bare in mind, patch will be working very specifically. Let say the version 3 Tb03.patch is use to patch from Tb02, if you apply patch on Tb01, sometimes it will corrupt your source code. So, to make sure it works, do a dry run. Dry-run means a fake-test, do it at the directory of the source code targeted to patch.

Doing dry-run like this:

patch --dry-run -p1 -i Tb02.patch

The success output looks like this:

patching file TbApi.cpp
patching file TbApi.h
patching file TbCard.cpp
...

The failure ouptut looks like this:

patching file TbCard.cpp
Hunk #2 FAILED at 585.
1 out of 2 hunks FAILED -- saving rejects to file TbCard.cpp.rej
patching file TbCard.h
Hunk #1 FAILED at 57.
Hunk #2 FAILED at 77.
Hunk #3 succeeded at 83 with fuzz 1 (offset -21 lines).
....

At last, if the dry-run is giving good result, do this and enjoy the compilation.

patch -p1 -i Tb02.patch

References:
1. Network Theory Ltd Patch Intro
2. How to create and use a patch in Linux

Sunday, January 8, 2012

Changing MAC address under Linux Command Line

[1] Ref


$ ifconfig -a | grep HWaddr
eth0  Link encap:Ethernet HWaddr 00:80:48:BA:d1:20
 

# ifconfig eth0 down
# ifconfig eth0 hw ether 00:80:48:BA:d1:30
# ifconfig eth0 up
# ifconfig eth0 |grep HWaddr
 
 
In Linux, Windows, Mac OS X, or a different operating system
changing MAC address is only temporary. 
 
Once you reboot your machine, the operating system reflects the 
physical MAC address burnt in your network card and not the MAC address you set.
 

Thursday, January 5, 2012

Set Time and Date under Linux

[1] Linuxsa.org

Setting the system clock

To set the system clock under Linux, use the date command. As an example, to set the current time and date to July 31, 11:16pm, type ``date 07312316'' (note that the time is given in 24 hour notation). If you wanted to change the year as well, you could type ``date 073123161998''. To set the seconds as well, type ``date 07312316.30'' or ``date 073123161998.30''. To see what Linux thinks the current local time is, run date with no arguments.

Setting the hardware clock

To set the hardware clock, my favourite way is to set the system clock first, and then set the hardware clock to the current system clock by typing ``/sbin/hwclock --systohc'' (or ``/sbin/hwclock --systohc --utc'' if you are keeping the hardware clock in UTC). To see what the hardware clock is currently set to, run hwclock with no arguments. If the hardware clock is in UTC and you want to see the local equivalent, type ``/sbin/hwclock --utc''

Wednesday, December 28, 2011

FW: File size limit exceeded error under Linux and solution

[1] From Cyberciti.biz

File size limit exceeded error under Linux and solution

$ulimit -a

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 227
virtual memory (kbytes, -v) unlimited

Wednesday, August 3, 2011

Change MAC address under Linux

[1] http://www.linuxquestions.org/questions/linux-software-2/how-to-change-mac-address-304234/

[root@localhost ltib]# ifconfig eth0 down hw ether 00:0C:F1:87:56:F8

Wednesday, July 6, 2011

CmdLine ----- Check Public IP address

[1] http://www.simplehelp.net/2009/04/07/how-to-find-your-public-ip-address-with-the-linux-command-line/

[root@GsnCommsModule /]# wget -q -O - http://checkip.dyndns.org
...Current IP Check Current IP Address: 1.152.83.93...

wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Monday, May 2, 2011

Unzip window zip file under Linux

[1] http://www.cyberciti.biz/tips/how-can-i-zipping-and-unzipping-files-under-linux.html