Linux Bests
#1. Create and change directory in a single command
[sanjeev@localhost ~]$ mkdir Code && cd $_
[sanjeev@localhost Code]$
#2 modify a user so that it could not login.
usermod -s /sbin/nologin <user name>
it will set the user's login directory to nologin and the user will not be able to login .
#3 Show the current firewall rules
iptables -L
#Put a process in backgroup and than getting it in foreground
1- lets write a sample python prog test.py
import time
for i in range(20): time.sleep(i)
2 - Execute the program python test.py
python test.py
3- Crtl+z will put the program in background
4- now to get the program back in the foreground, type
fg
#How to know the exit status of the last command
echo $?
################################################################################
awk commands
################################################################################
Below is a file sample.txt
-rw-rw-r--. 1 sr sr 12250696 May 23 2015 Python-2.7.10.tar.xz
-rw-rw-r--. 1 sr sr 12059 Mar 10 2016 ez_setup.py
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Videos
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Templates
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Public
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Pictures
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Music
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Downloads
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Documents
drwxr-xr-x. 2 sr sr 4096 Mar 10 2016 Desktop
drwxr-xr-x. 18 sr sr 4096 Mar 11 2016 Python-2.7.10
-rw-rw-r--. 1 sr sr 722669 Mar 11 2016 setuptools-20.2.2.zip
drwxrwxr-x. 3 sr sr 4096 Mar 11 2016 services
-rw-rw-r--. 1 sr sr 733319 Mar 11 2016 ansible-tower-setup-latest.gz
drwxrwxr-x. 6 sr sr 4096 Mar 11 2016 ansible-tower-setup-2.4.3
drwxrwxr-x. 2 sr sr 4096 Jul 19 19:45 progs
drwxrwxr-x. 2 sr sr 4096 Jul 24 00:14 crTest
drwxrwxr-x. 5 sr sr 4096 Jul 24 06:07 project1
drwxrwxr-x. 3 sr sr 4096 Jul 27 05:59 todo-api
-rw-rw-r--. 1 sr sr 0 Aug 2 07:13 a.txt
drwxrwxr-x. 3 sr sr 4096 Aug 9 06:01 a
-rw-rw-r--. 1 sr sr 15 Aug 9 10:59 serSt.retry
-rw-rw-r--. 1 sr sr 0 Aug 9 11:40 man
-rw-rw-r--. 1 sr sr 6 Aug 9 11:41 hello
-rwxrwxr-x. 1 sr sr 135 Aug 9 12:12 check_email.sh
-rw-rw-r--. 1 sr sr 0 Aug 24 06:39 some.txt
drwxrwxr-x. 2 sr sr 4096 Sep 6 06:53 commandTest
-rw-rw-r--. 1 sr sr 200 Sep 8 00:15 test.py
drwxrwxr-x. 7 sr sr 4096 Sep 10 13:42 AnsibleTest
drwxrwxr-x. 2 sr sr 4096 Sep 14 16:27 ed
drwxrwxr-x. 2 sr sr 4096 Sep 14 16:27 cd
drwxrwxr-x. 4 sr sr 4096 Sep 14 16:29 testGit
-rw-rw-r--. 1 sr sr 602 Oct 4 00:53 llNexi.py
drwxrwxr-x. 3 sr sr 4096 Oct 5 16:47 repos
drwxrwxr-x. 4 sr sr 4096 Oct 6 01:43 Code
-rw-rw-r--. 1 sr sr 0 Oct 17 17:13 sample.txt
Below are two AWK output samples on the given text -
#1 - Below will print the column 1 of all the rows
awk '{print $1}' sample.txt
#2 - Print all the file those are created in the month of march
The below command will split the file on delimiter blank " " (-F " ") and print the name of the file if it has been created in the month of march, likely few other combinations are possible as required.
awk -F " " '{if ($6=="Mar") print $9}' sample.txt
[sanjeev@localhost ~]$ awk -F " " '{if ($6=="Mar") print $9}' sample.txt
ez_setup.py
Videos
Templates
Public
Pictures
Music
Downloads
Documents
Desktop
Python-2.7.10
setuptools-20.2.2.zip
services
ansible-tower-setup-latest.gz
ansible-tower-setup-2.4.3
[sanjeev@localhost ~]$
###############################################################
Sed - now lets look at few basic sed commands
###############################################################
sed -e 's/sr/currentLU/g' sample.txt
[sanjeev@localhost ~]$ sed -e 's/sr/currentLinuxUser/' sample.txt
-rw-rw-r--. 1 currentLinuxUser sr 12250696 May 23 2015 Python-2.7.10.tar.xz
-rw-rw-r--. 1 currentLinuxUser sr 12059 Mar 10 2016 ez_setup.py
drwxr-xr-x. 2 currentLinuxUser sr 4096 Mar 10 2016 Videos
drwxr-xr-x. 2 currentLinuxUser sr 4096 Mar 10 2016 Templates
drwxr-xr-x. 2 currentLinuxUser sr 4096 Mar 10 2016 Public
drwxr-xr-x. 2 currentLinuxUser sr 4096 Mar 10 2016 Pictures
drwxrwxr-x. 4 currentLinuxUser sr 4096 Sep 14 16:29 testGit
-rw-rw-r--. 1 currentLinuxUser sr 602 Oct 4 00:53 llNexi.py
drwxrwxr-x. 3 currentLinuxUser sr 4096 Oct 5 16:47 repos
drwxrwxr-x. 4 currentLinuxUser sr 4096 Oct 6 01:43 Code
-rw-rw-r--. 1 currentLinuxUser sr 0 Oct 17 17:13 sample.txt
[sanjeev@localhost ~]$
[sanjeev@localhost ~]$ sed -e 's/sr/currentLinuxUser/g' sample.txt
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 12250696 May 23 2015 Python-2.7.10.tar.xz
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 12059 Mar 10 2016 ez_setup.py
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Videos
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Templates
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Public
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Pictures
drwxrwxr-x. 4 currentLinuxUser currentLinuxUser 4096 Sep 14 16:29 testGit
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 602 Oct 4 00:53 llNexi.py
drwxrwxr-x. 3 currentLinuxUser currentLinuxUser 4096 Oct 5 16:47 repos
drwxrwxr-x. 4 currentLinuxUser currentLinuxUser 4096 Oct 6 01:43 Code
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 0 Oct 17 17:13 sample.txt
The above command will show the changes on the screen itelf, but to make them permanent in the file
use -i, like below, and it will will changes permanent to the input file itself
[sanjeev@localhost ~]$ sed -i 's/sr/currentLinuxUser/g' sample.txt
[sanjeev@localhost ~]$
[sanjeev@localhost ~]$ cat sample.txt
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 12250696 May 23 2015 Python-2.7.10.tar.xz
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 12059 Mar 10 2016 ez_setup.py
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Videos
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Templates
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Public
drwxr-xr-x. 2 currentLinuxUser currentLinuxUser 4096 Mar 10 2016 Pictures
drwxrwxr-x. 4 currentLinuxUser currentLinuxUser 4096 Sep 14 16:29 testGit
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 602 Oct 4 00:53 llNexi.py
drwxrwxr-x. 3 currentLinuxUser currentLinuxUser 4096 Oct 5 16:47 repos
drwxrwxr-x. 4 currentLinuxUser currentLinuxUser 4096 Oct 6 01:43 Code
-rw-rw-r--. 1 currentLinuxUser currentLinuxUser 0 Oct 17 17:13 sample.txt
[sanjeev@localhost ~]$
Comments
Post a Comment