Saturday, May 28, 2016

10 Simple ways to work with the `ls` command in Linux


So, today we'll work with the `ls` command in Linux
& see how it can help us to know about directories & files on our system.

`ls` helps us list the contents of the directory/folder that we are currently in, or that we specify as a parameter to `ls`, and lets us understand about other details of the files/directories like, the permissions on them, owners/groups they belong to, 
last modified times on them, their size with some other functionalities.

Listing names of the contents in the current folder

ls 
 1.txt  b.b  bench  bench_apache  bench_nginx  bench.sh


Listing contents with summary (Name and size in Kilobytes) 

ls -s
total 20
0 1.txt  4 b.b  4 bench  4 bench_apache  4 bench_nginx  4 bench.sh

Listing contents with long list format (Name and size in Kilobytes) (It is kilobytes)

ls -l
total 20
-rw-rw-r-- 1 ash ash    0 May 22 19:04 1.txt
drwxrwxr-x 2 ash ash 4096 May 29 00:03 b.b
drwxrwxr-x 3 ash ash 4096 May 22 19:30 bench
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_apache
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_nginx
-rwx------ 2 ash ash  243 Mar 10  2014 bench.sh

Listing in long listing & human readable format

ls -lh
-rw-rw-r-- 1 ash ash    0 May 22 19:04 1.txt
drwxrwxr-x 2 ash ash 4.0K May 29 00:03 b.b
drwxrwxr-x 3 ash ash 4.0K May 22 19:30 bench
-rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_apache
-rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_nginx
-rwx------ 2 ash ash  243 Mar 10  2014 bench.sh

Listing in long listing & human readable format

ls -lsh
total 20K
   0 -rw-rw-r-- 1 ash ash    0 May 22 19:04 1.txt
4.0K drwxrwxr-x 2 ash ash 4.0K May 29 00:03 b.b
4.0K drwxrwxr-x 3 ash ash 4.0K May 22 19:30 bench
4.0K -rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_apache
4.0K -rw-r--r-- 1 ash ash 1.4K Mar 10  2014 bench_nginx
4.0K -rwx------ 2 ash ash  243 Mar 10  2014 bench.sh

Ignoring files & only listing directories

(Caveat: This will ignore any file/directory which contains a .(dot) in its name. Sometime, directories contain a dot in their name so they might get ignored as well so watch out, and sometimes, files do not contain a dot in their name)
ls -lI "*.*"
drwxrwxr-x 3 ash ash 4096 May 22 19:30 bench
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_apache
-rw-r--r-- 1 ash ash 1401 Mar 10  2014 bench_nginx


Listing files and folders in the current directory {Having a dot (.) in their names}

ls *.*
1.txt  bench.sh

b.b:
b1.txt


List the files & folders in the current directory's, & further files/folders inside the folders in current directory (to Depth 1)

ls *
1.txt  bench_apache  bench_nginx  bench.sh

b.b:
b1.txt

bench:
b  b.txt

List all files along with hidden files

ls -a
.  ..  1.txt  b.b  bench  bench_apache  bench_nginx  bench.sh  .hidden

List Files & Folder contents starting with a Name/Pattern/Alphabet

Eg. List all the files starting with the alphabet 'b' & folder contents starting with the alphabet 'b'
ls b*
bench_apache  bench_nginx  bench.sh

b.b:
b1.txt

bench:
b  b.txt

Thursday, May 12, 2016

A few things to do with screen command and screen session in Linux

Today we'll try to see a few things we can do with the screen command.
If you wish to have multiple bash terminals running in the same terminal window 
without creating multiple terminal windows or terminal tabs, 
screen is the command you would want to aim at.

So let's see what options does screen command gives us:
screen --help

And, we get the following details about the screen command.




Currently running screen sessions

screen -ls


Attaching a Detached screen sessions

If a screen session was detached from a bash terminal window, 
and you wish your current bash window, to attach the that screen session process, 
you can just type the following.
screen -r <process_id.your_screen_session_name>

You could also use only the your_screen_session_name without the process_id if the multiple screen sessions are named differently, like this:
screen -r <your_screen_session_name>

Detaching & Attaching an attached screen sessions

If you were disconnected from a screen session somehow, and the screen session never got detached,
you can detach it from the bash terminal where it is attached to, & attach it to your current bash terminal window, 
and start working with it.
screen -D -r <process_id.your_screen_session_name>


Killing the whole screen session

To kill the whole screen session, type:
Ctrl-a \


To Kill a window in a screen session

Let's start a screen session with total 3 windows created
screen -S Local_Server -t my_local_server_1
Type the following 2 times, to create 2 more screen windows:
Ctrl-a c
Now to kill a window in screen,  go to that window
Since you created 2 more screen window in the same bash terminal, by typing the above command 2 times.
You will now have total 3 windows listed, starting from the number 0 to 2, inside screen type:
Ctrl-a Shift-"
We can see 3 Windows like this




To scroll up the window in screen

If a lot of output has been printed on your terminal while in a screen session,
and you would want to scroll up & see what got printed, you can type:
Ctrl-a Esc k
Ctrl-a: The escape character
Esc: The Escape key, to enter into scroll mode
k: To scroll up
Others you can use:
j: scroll up
h: move cursor towards left
l: move cursor towards right
You could also use the arrow keys instead of, j,k,h,l.
Type Esc once key to exit scrolling.


Changing the escape character in screen from Ctrl-a to other

Open the ~/.screenrc file that you should be having in your home folder (/home/your_username/.screenrc), if you don't then you can just create it.
Let's change the escape character from Ctrl-a to Ctrl-z
Enter the following in your ~/.screenrc and save it.
escape ^zz
Now start screen & your escape key has been changed to Ctrl-z from Ctrl-a