The command that I've been using is "ls -lR". The results usually look like this:
.:
total 4
lrwxrwxrwx 1 root root 9 Oct 11 03:35 dos -> /root/dos
drwxr-xr-x 2 root root 80 Oct 11 03:35 folder1
drwxr-xr-x 2 root root 100 Oct 11 03:35 folder2
-rw-r--r-- 1 root root 242 Oct 11 03:35 hello.c
./folder1:
total 0
-rw-r--r-- 1 root root 0 Oct 11 03:25 file1001
-rw-r--r-- 1 root root 0 Oct 11 03:35 file1002
./folder2:
total 0
-rw-r--r-- 1 root root 0 Oct 11 03:39 file2001
-rw-r--r-- 1 root root 0 Oct 11 03:45 file2002
How do I optimize the command so that it would only display the following?
./folder1:
Oct 11 03:25 | file1001
Oct 11 03:35 | file1002
./folder2:
Oct 11 03:39 | file2001
Oct 11 03:45 | file2002
Here's something that might work:
~/mydir ls -lR | grep -vi "total" | egrep -o "^\.\/.*|^\..*|([A-Z])\w+\s+[0-9]+\s+[0-9]+\s+.*"
.:
Sep 4 2015 es_jira.py
Sep 4 2015 es_slurp_jira.py
Aug 21 2015 __init__.py
./plugins:
Sep 4 2015 __init__.py
Sep 4 2015 lrt_report.py
Sep 11 2015 mr_fingerprint.py
Mar 6 2016 mr_tunable.py
Dec 1 2015 plugin.py
Dec 1 2015 test
Dec 1 2015 utils.py
./plugins/test:
Sep 4 2015 _test_ca_space_plugin.py
Sep 4 2015 _test_lrt_report_plugin.py
Sep 4 2015 _test_mr_failover_plugin.py
Sep 4 2015 _test_mr_fingerprint_plugin.py
Dec 1 2015 _test_mr_tunable_plugin.py
Sep 4 2015 _test_spacedays_plugin.py
If you want to start adding tabs for nested lines and stuff, you're looking for a script and variable work, which is doable in a one-liner, but gets more complicated than a quick and dirty grep.
Related
I am trying to create files with different date created:
$ touch -t 20{11..15}01120000 file_{1..5}.txt
$ ls -al
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 201201120000
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 201301120000
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 201401120000
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 201501120000
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 file_1.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 file_2.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 file_3.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 file_4.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 file_5.txt
As you can see all file's dates created are 2011 Jan 12 0.
How can I create files with a different year?
# this is what I want
-rw-r--r-- 1 shinokada staff 0 Jan 12 2011 file_1.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2012 file_2.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2013 file_3.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2014 file_4.txt
-rw-r--r-- 1 shinokada staff 0 Jan 12 2015 file_5.txt
What is the best way?
touch command allows you to enter one one timestamp using -t option.
A traditional for-loop would be better:
for i in {1..5}; do touch -t 201${i}01120000 file_$i.txt; done
Shortly, but keeping filenumbers and years as separated variables
I think this is more readable, but...
Care about timezone!
filenum=1
for year in {2011..2015};do
TZ=UTC touch -t ${year}12312345 file-$((filenum++))
done
Then if you look about this, using a different timezone:
(Note that created date was Dec 31, 23h45')
TZ=UTC-1 ls -ltr
-rw-r--r-- 1 user user 0 jan 1 2012 file-1
-rw-r--r-- 1 user user 0 jan 1 2013 file-2
-rw-r--r-- 1 user user 0 jan 1 2014 file-3
-rw-r--r-- 1 user user 0 jan 1 2015 file-4
-rw-r--r-- 1 user user 0 jan 1 2016 file-5
Could you please try following, I would go with following approach with a for loop. Where I am providing year, number of files which needed, output file's initial value and same time value for all output files so that we can manage it in for loop.
cat script.bash
year=2011
numberoffiles="10"
time="01120000"
outputfileInitials="file"
nameSequence="1"
for ((i = 1 ; i <= numberoffiles ; i++ ));
do
touch -t $year$time "${outputfileInitials}_$nameSequence.txt"
(( nameSequence = nameSequence + 1 ))
(( year = year + 1 ))
done
I intent to enumerate the files and dirs
[root#iz2ze9wve43n2nyuvmsfx5z /]# ls -lh |nl
1 total 60K
2 lrwxrwxrwx 1 root root 7 Jun 15 22:08 bin -> usr/bin
3 dr-xr-xr-x. 5 root root 4.0K Jun 15 22:10 boot
4 drwxr-xr-x 19 root root 2.9K Sep 17 11:35 dev
5 drwxr-xr-x. 85 root root 4.0K Sep 18 22:44 etc
6 drwxr-xr-x. 2 root root 4.0K Sep 17 08:25 home
7 lrwxrwxrwx 1 root root 7 Jun 15 22:08 lib -> usr/lib
8 lrwxrwxrwx 1 root root 9 Jun 15 22:08 lib64 -> usr/lib64
9 drwx------. 2 root root 16K Oct 15 2017 lost+found
10 drwxr-xr-x. 2 root root 4.0K Apr 11 2018 media
11 drwxr-xr-x. 2 root root 4.0K Apr 11 2018 mnt
12 drwxr-xr-x. 2 root root 4.0K Apr 11 2018 opt
13 dr-xr-xr-x 86 root root 0 Sep 17 11:35 proc
14 drwxrwxrwx. 21 root root 4.0K Oct 22 08:11 root
15 drwxr-xr-x 24 root root 660 Oct 24 19:10 run
16 lrwxrwxrwx 1 root root 8 Jun 15 22:08 sbin -> usr/sbin
17 drwxr-xr-x. 2 root root 4.0K Apr 11 2018 srv
18 dr-xr-xr-x 13 root root 0 Oct 21 19:50 sys
19 drwxrwxrwt. 11 root root 4.0K Oct 24 05:19 tmp
20 drwxr-xr-x. 13 root root 4.0K Jun 15 22:08 usr
21 drwxr-xr-x. 19 root root 4.0K Jun 15 22:08 var
It starts with 1 which I desire is 0.
How could make nl to enumerate from a specified line.
You just need to use the -v option, and specify to start from 0; your instruction becoming:
ls -lh |nl -v 0
Lets have a look at nl --help:
$ nl --help
Usage: nl [OPTION]... [FILE]...
Write each FILE to standard output, with line numbers added.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
...
-v, --starting-line-number=NUMBER first line number for each section
Therefore:
$ nl -v0 <<EOF
> a
> b
> EOF
0 a
1 b
Note that it does not really matter whether you want to enumerate directories or anything else, which is why you probably should strip this part from your question to make it more specific.
Can I change the modified date, just the year, of a file or all files?
I have been looking here which lead me to touch.
$ ls -l *.txt
-rw-r--r-- 1 kevin.smith mkpasswd 3319 Nov 21 2017 adjectives.txt
-rw-r--r-- 1 kevin.smith mkpasswd 25562 Aug 11 2015 checklist.txt
-rwxr-xr-x 1 kevin.smith mkpasswd 11347 May 9 14:28 cw_text.txt
-rw-r--r-- 1 kevin.smith mkpasswd 9260 May 9 14:31 cw_text2.txt
-rw-r--r-- 1 kevin.smith mkpasswd 4786 May 9 14:38 cw_text3.txt
-rw-r--r-- 1 kevin.smith mkpasswd 390 Jun 25 2014 Delete_log.txt
-rw-r--r-- 1 kevin.smith mkpasswd 6891 Jul 27 2015 log.txt
-rw-r--r-- 1 kevin.smith mkpasswd 53828 Jan 17 2017 pin1.txt
-rw-r--r-- 1 kevin.smith mkpasswd 39412 Jan 17 2017 pip2.txt
-rw-r--r-- 1 kevin.smith mkpasswd 167 Dec 5 2015 romeo.txt
$ touch -t 2018* *.txt
Expected Output: Would have only the year changed to 2018
$ ls -l *.txt
-rw-r--r-- 1 kevin.smith mkpasswd 3319 Nov 21 2018 adjectives.txt
-rw-r--r-- 1 kevin.smith mkpasswd 25562 Aug 11 2018 checklist.txt
-rwxr-xr-x 1 kevin.smith mkpasswd 11347 May 9 14:28 cw_text.txt
-rw-r--r-- 1 kevin.smith mkpasswd 9260 May 9 14:31 cw_text2.txt
-rw-r--r-- 1 kevin.smith mkpasswd 4786 May 9 14:38 cw_text3.txt
-rw-r--r-- 1 kevin.smith mkpasswd 390 Jun 25 2018 Delete_log.txt
-rw-r--r-- 1 kevin.smith mkpasswd 6891 Jul 27 2018 log.txt
-rw-r--r-- 1 kevin.smith mkpasswd 53828 Jan 17 2018 pin1.txt
-rw-r--r-- 1 kevin.smith mkpasswd 39412 Jan 17 2018 pip2.txt
-rw-r--r-- 1 kevin.smith mkpasswd 167 Dec 5 2018 romeo.txt
ls -l file.txt; touch -t "$(date -d "#$(stat -c '%Y' file.txt)" "+2020%m%d%H%M")" file.txt; ls -l file.txt
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-rw-r--r-- 1 jackman jackman 0 Oct 23 16:47 file.txt
-rw-r--r-- 1 jackman jackman 0 Oct 23 2020 file.txt
You'll need to use a for loop to iterate over the files, and query/update the mtime one-by-one
amar#pc:/local/mnt/workspace/amarn$ gcc -m32 a.c
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
amar#pc:/local/mnt/workspace/amarn$ cat a.c
#include <stdio.h>
int
main ()
{
long z;
printf("Long int size is %d bytes long!\n", sizeof(z));
;
return 0;
}
amarn#hyd-tbsbld01:/local/mnt/workspace/amarn$ uname -a
Linux mypc 4.4.0-78-generic #99-Ubuntu SMP Thu Apr 27 15:29:09 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
I made following change for gcc.
gcc was mapped to gcc-5.
I mapped gcc to gcc-4.8.
amar#mypc:/usr/bin$ ll | grep gcc
-rwxr-xr-x 1 root root 428 May 7 2006 c89-gcc
-rwxr-xr-x 1 root root 454 Apr 11 2011 c99-gcc
lrwxrwxrwx 1 root root 7 Jun 1 11:54 gcc -> gcc-4.8
-rwxr-xr-x 1 root root 776344 Jan 26 2016 gcc-4.8
-rwxr-xr-x 1 root root 915736 Nov 3 2016 gcc-5
lrwxrwxrwx 1 root root 10 Jun 1 11:55 gcc-ar -> gcc-ar-4.8
-rwxr-xr-x 1 root root 27032 Jan 26 2016 gcc-ar-4.8
-rwxr-xr-x 1 root root 31136 Nov 3 2016 gcc-ar-5
lrwxrwxrwx 1 root root 10 Jun 1 11:56 gcc-nm -> gcc-nm-4.8
-rwxr-xr-x 1 root root 27032 Jan 26 2016 gcc-nm-4.8
-rwxr-xr-x 1 root root 31136 Nov 3 2016 gcc-nm-5
lrwxrwxrwx 1 root root 14 Jun 1 11:56 gcc-ranlib -> gcc-ranlib-4.8
-rwxr-xr-x 1 root root 27032 Jan 26 2016 gcc-ranlib-4.8
-rwxr-xr-x 1 root root 31136 Nov 3 2016 gcc-ranlib-5
lrwxrwxrwx 1 root root 7 Jun 1 12:32 x86_64-linux-gnu-gcc -> gcc-4.8
lrwxrwxrwx 1 root root 7 Jan 26 2016 x86_64-linux-gnu-gcc-4.8 -> gcc-4.8
lrwxrwxrwx 1 root root 5 Nov 3 2016 x86_64-linux-gnu-gcc-5 -> gcc-5
lrwxrwxrwx 1 root root 10 Jun 1 12:34 x86_64-linux-gnu-gcc-ar -> gcc-ar-4.8
lrwxrwxrwx 1 root root 10 Jan 26 2016 x86_64-linux-gnu-gcc-ar-4.8 -> gcc-ar-4.8
lrwxrwxrwx 1 root root 8 Nov 3 2016 x86_64-linux-gnu-gcc-ar-5 -> gcc-ar-5
lrwxrwxrwx 1 root root 10 Jun 1 12:34 x86_64-linux-gnu-gcc-nm -> gcc-nm-4.8
lrwxrwxrwx 1 root root 10 Jan 26 2016 x86_64-linux-gnu-gcc-nm-4.8 -> gcc-nm-4.8
lrwxrwxrwx 1 root root 8 Nov 3 2016 x86_64-linux-gnu-gcc-nm-5 -> gcc-nm-5
lrwxrwxrwx 1 root root 14 Jun 1 12:35 x86_64-linux-gnu-gcc-ranlib -> gcc-ranlib-4.8
lrwxrwxrwx 1 root root 14 Jan 26 2016 x86_64-linux-gnu-gcc-ranlib-4.8 -> gcc-ranlib-4.8
lrwxrwxrwx 1 root root 12 Nov 3 2016 x86_64-linux-gnu-gcc-ranlib-5 -> gcc-ranlib-5
Based on a list generated with the command
find '/patch' -name A* -exec ls -la {} \;
Get only a list with file names that appears more than once (duplicated), but I want display the full line not only the file name.
File name consider only the name of file not the full path.
Example
Based on this file:
-rw-r--r-- 1 root root 34K Jan 9 2014 /usr/share/dia/sheets/AADL.sheet
-rw-r--r-- 1 root root 952 Fev 14 07:07 /usr/share/postgresql/9.3/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 955 Jul 30 2014 /usr/share/postgresql/9.1/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 92K Abr 28 2014 /usr/share/gettext/ABOUT-NLS
-rw-r--r-- 1 root root 545 Dez 14 2013 /usr/share/dia/sheets/Automata.sheet
-rw-r--r-- 1 root root 6,7K Dez 21 2012 /usr/share/perl5/Mail/Address.pm
-rw-r--r-- 1 root root 709 Mar 3 09:03 /home/test/Address.pm
-rw-r--r-- 1 root root 709 Mar 3 11:13 /home/test/Automata.sheet
-rw-r--r-- 1 root root 520 Mar 3 11:15 /home/test/t2/Address.pm
I want get this result:
-rw-r--r-- 1 root root 952 Fev 14 07:07 /usr/share/postgresql/9.3/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 955 Jul 30 2014 /usr/share/postgresql/9.1/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 6,7K Dez 21 2012 /usr/share/perl5/Mail/Address.pm
-rw-r--r-- 1 root root 709 Mar 3 09:03 /home/test/Address.pm
-rw-r--r-- 1 root root 520 Mar 3 11:15 /home/test/t2/Address.pm
-rw-r--r-- 1 root root 545 Dez 14 2013 /usr/share/dia/sheets/Automata.sheet
-rw-r--r-- 1 root root 709 Mar 3 11:13 /home/test/Automata.sheet
Using this commands
awk -F. '{ n = split($0, a, "/"); print a[n] }' file |sort | uniq -d > filedups
I got
ABORT.7.gz
Address.pm
Automata.sheet
and after
grep -f filedups file
I get expected result.
My question:
Is there a direct way to do this in just one line using awk and/or other commands?
awk to the rescue!
starting with your initial file
$ awk '{n=split($NF,a,"/"); k=a[n]; c[k]++;
v[k]=k in v?v[k] ORS $0:$0}
END {for(k in c) if(c[k]>1) print v[k]}' file
-rw-r--r-- 1 root root 6,7K Dez 21 2012 /usr/share/perl5/Mail/Address.pm
-rw-r--r-- 1 root root 709 Mar 3 09:03 /home/test/Address.pm
-rw-r--r-- 1 root root 520 Mar 3 11:15 /home/test/t2/Address.pm
-rw-r--r-- 1 root root 545 Dez 14 2013 /usr/share/dia/sheets/Automata.sheet
-rw-r--r-- 1 root root 709 Mar 3 11:13 /home/test/Automata.sheet
-rw-r--r-- 1 root root 952 Fev 14 07:07 /usr/share/postgresql/9.3/man/man7/ABORT.7.gz
-rw-r--r-- 1 root root 955 Jul 30 2014 /usr/share/postgresql/9.1/man/man7/ABORT.7.gz