Sunday, June 29, 2014

Unix Shell: List Files

1. list files 
terminal:
 1) echo command can be used to print out the file names matching the given expression
 2) ls command can print out all files whose name starting with t
 3) ls command can port the result to command cat with pipeline
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo t*  
 test test1 test3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls t*  
 test test1 test3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls t* | cat  
 test  
 test1  
 test3  

2. list files in one column
terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -1  
 2  
 test  
 test1  
 test3  

3. list files not existed
terminal:
1) ls need to ensure the given file exists
2) echo doesn't need to ensure that. If given file not exists, it will just 
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls k*  
 ls: cannot access k*: No such file or directory  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo k*  
 k*  

4. Provide no files to list
terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo *  
 2 test test1 test3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls *  
 2 test test1 test3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo  
   
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls  
 2 test test1 test3  

5. List Hidden Files
Hidden files are files whose name starts with "."(dot)
terminal:
1) touch command is used to create temporary empty files
2) since all files are hidden, then "echo *" can't be used to echo all files, it will just take "*" as the string to print out
3) because all files are hidden, ls * will just files whose name is "*", and there is no such file
4) ls with nothing will just try to list all files, but nothing get found
5) But hidden files can be found by ".*", meaning any files whose names starting with a "."(dot). Besides three files created by touch, we also have following 2 files:
. : represents current directory
.. : represents the parent directory
6) The first line is a list of all hidden files, the 2nd and 3rd lines are content of "local directory" and content of "parent directory" , including all files/directories these places contain
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch .one .two .three  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo *  
 *  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls *  
 ls: cannot access *: No such file or directory  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo .*  
 . .. .one .three .two  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls .*  
 .one .three .two  
   
 .:  
   
 ..:  
 h1 h2 xxdev  

6. List contents of directory itself
sometimes, when running "ls .*" or other similar commands, we don't want to list too much details of other directories, we just want to focus on current directory, we can use -d option
terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls .*  
 .one .three .two  
   
 .:  
   
 ..:  
 h1 h2 xxdev  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -d .*  
 . .. .one .three .two  

7. List all files(including hidden files)
terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -a  
 . .. .one .three .two  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$  

8. List files with more information
terminal:
first column: first character: d means directory, - means ordinary file, l means symbolic links. Next 9 characters: file permissions for user, group and other. r: read, w: write, x: execution, - if the permission is absent.

2nd column:  link count. Only folder is a directory, it is pointing to another place.

3rd and 4th column: file owner and group

5th column: file size in bytes

6th 7th and 8th column: last modification time
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -l  
 total 4  
 drwxrwxr-x 2 aubinxia aubinxia 4096 Jun 29 15:23 folder  
 -rw-rw-r-- 1 aubinxia aubinxia  0 Jun 29 15:22 test1  
 -rw-rw-r-- 1 aubinxia aubinxia  0 Jun 29 15:22 test2  
 -rw-rw-r-- 1 aubinxia aubinxia  0 Jun 29 15:22 test3  

No comments:

Post a Comment