2016年3月10日 星期四

[Linux 常見問題] How to output only file names (with spaces) in ls -Al?

Source From Here 
Question 
As title, I want to extract the file name only. For example: 
# ls -hl var/
總計 0
drwxr-xr-x. 3 root root 16 2月 19 10:00 C1
drwxr-xr-x. 2 root root 6 2月 19 09:43 C2
drwxr-xr-x. 2 root root 19 2月 19 09:52 C3
drwxr-xr-x. 2 root root 19 2月 19 10:01 C4

# ls -hl var/ | awk '{print $9}'

C1
C2
C3
C4

# ls -Al var/ | grep -v '總計' | awk '{print $9}' // Remove the empty line
C1
C2
C3
C4

I need very simple solution. Maybe there is better solution than awk. 

How-To 
Leverage command find to make job easy: 
# find ./var/ -name C* -printf "%f\n"
C1
C2
C3
C4

Here we just use argument -printf format to fit our requirement: 
print format on the standard output, interpreting `\' escapes and `%' directives. Field widths and precisions can be specified as with the `printf' C function. Unlike -print, -printf does not add a newline at the end of the string.


Supplement 
[Linux 文章收集] Awk Introduction Tutorial – 7 Awk Print Examples

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...