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

標籤

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

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!