12

I'm trying to create a zip file that will run on multiple servers to compare the contents of the servers. For many reasons, the easiest and best piece of information for me to compare is a text file of the directory listings... but I need that without the modified date as they make the comparisons show differences that don't matter in this case.

So if I run command ls -la to create text output for comparing, I get would get something like this:

drwxr-xr-x.  6 root   root       4096 Mar 20 14:59 .
dr-xr-xr-x. 22 root   root       4096 Feb 18 03:20 ..
drwxr-xr-x.  9  web   ad         4096 Oct 30 14:35 apache-tomcat-6.0.18
drwxr-xr-x.  9  web   ad         4096 Mar 24 03:00 apache-tomcat-6.0.36
lrwxrwxrwx.  1 root   root          5 Oct 30 14:06 java -> java6
lrwxrwxrwx.  1 root   root         16 Oct 30 14:05 java6 -> jdk/jdk1.6.0_37/
lrwxrwxrwx.  1 root   root         16 Mar 20 14:59 java7 -> jdk/jdk1.7.0_17/
drwxr-xr-x.  4 root   root       4096 Mar 20 15:02 jdk
lrwxrwxrwx.  1 root   root         21 Nov  6 15:09 tomcat -> apache-tomcat-6.0.36/

What I would like is to use is ls -la | cut -c 1-31 but that only takes one list (the characters 1-31) and I really want the data after the date. I'm fairly new with Unix and was curious if anyone knows how to produce a list that would look something like this:

drwxr-xr-x.  6 root   root       4096 .
dr-xr-xr-x. 22 root   root       4096 ..
drwxr-xr-x.  9  web   ad         4096 apache-tomcat-6.0.18
drwxr-xr-x.  9  web   ad         4096 apache-tomcat-6.0.36
lrwxrwxrwx.  1 root   root          5 java -> java6
lrwxrwxrwx.  1 root   root         16 java6 -> jdk/jdk1.6.0_37/
lrwxrwxrwx.  1 root   root         16 java7 -> jdk/jdk1.7.0_17/
drwxr-xr-x.  4 root   root       4096 jdk
lrwxrwxrwx.  1 root   root         21 tomcat -> apache-tomcat-6.0.36/

Thanks

Greg
  • 335
  • 1
  • 3
  • 10
  • 1
    You could filter the output of `ls`, but that can be unreliable, since `ls` output isn't really designed to be machine-readable. If you have GNU `find`, the `-printf` option lets you selectively print information in just about any format you like. – Keith Thompson Mar 25 '13 at 19:57

3 Answers3

22

Posting an alternate solution because the accepted one will fail on filename with spaces.

On Linux and other systems that support it (not Mac OS) use --time-style with null formatting

[root@preg junk]# ls -lah
total 12K
drwxr-xr-x 2 root root 4.0K Jan  6 16:12 .
drwxr-xr-x 4 root root 4.0K Jan  6 15:38 ..
-rw-r--r-- 1 root root    0 Jan  6 12:42 '$#%bad'
-rw-r--r-- 1 root root    2 Jan  6 16:12 'moar bad;'
-rw-r--r-- 1 root root    0 Jan  6 12:40 'quote"file'
-rw-r--r-- 1 root root    0 Jan  6 12:41 'single'\''quote'\''file'

[root@preg junk]# ls -lah  --time-style='+'  .
total 12K
drwxr-xr-x 2 root root 4.0K  .
drwxr-xr-x 4 root root 4.0K  ..
-rw-r--r-- 1 root root    0  '$#%bad'
-rw-r--r-- 1 root root    2  'moar bad;'
-rw-r--r-- 1 root root    0  'quote"file'
-rw-r--r-- 1 root root    0  'single'\''quote'\''file'
[root@preg junk]# 
111
  • 1,788
  • 1
  • 23
  • 38
14

I think the best solution to your problem is to use awk.

ls -la | awk '{print $1, $2, $3, $4, $5, $9}'

So you obtain an output like this:

drwxr-xr-x. 6 root root 4096 .
dr-xr-xr-x. 22 root root 4096 ..
drwx------+ 5 user staff 170 Desktop
drwx------+ 16 user staff 544 Documents
drwx------+ 6 user staff 204 Downloads
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Fabrizio Duroni
  • 747
  • 1
  • 11
  • 24
  • this works great! Thanks. I always knew I needed to expand my knowledge into awk. Thanks. – Greg Mar 25 '13 at 20:21
  • 2
    unfortunately this will fail on files with spaces in names like 'moar bad;' -- ls -ld moar\ bad\; | awk '{print $1, $2, $3, $4, $5, $9}' -rw-r--r-- 1 root root 2 moar Another possible solution: ls -ld --time-style='+' . use time-style with null formatting – 111 Jan 06 '17 at 21:00
-1

cut let's you specify ranges, more easily than awk.
Use it in combination with tr -s ' ' to trim repeating whitespaces.

$ ls -la /etc/ |tr -s ' ' |cut -d' ' -f1,2,3,4,5,9-
drwxr-xr-x 2 root root 4096 opt
lrwxrwxrwx 1 root root 21 os-release -> ../usr/lib/os-release
-rw-r--r-- 1 root root 6920 overlayroot.conf
drwxr-xr-x 4 root root 4096 vmware-tools
lrwxrwxrwx 1 root root 23 vtrgb -> /etc/alternatives/vtrgb

This is useful e.g. when using a parallel shell and you want to check files on multiple hosts, created at different times.

Alberto Chiusole
  • 2,204
  • 2
  • 16
  • 26