select fields with sed - bash

Using only grep and sed, is there a way I can tranform the output of ls -l * into this :
-rw-r--r-- agenda.txt
-rw-r--r-- annuaire.txt
Thanks!

seeing that you have already got your "answer", here's one of the simpler solution
ls -l | tr -s " "| cut -d" " -f1,8-
#OP, sed is "powerful", but sometimes, simplicity is more powerful.
Side note: Don't parse file names like that.

ls -l | sed 's/[ ]+//g' | sed 's/ [0-9].*:.[0-9]/ /g'

ls -altrh| sed -E 's/ +.+ / / g'
Or you can go with ssed which supports Perl Regular Expressions.
I solved your problem using the ssed program you can install it in any Posix system, ssed stands for super sed.
so i did a ls -latrh in my home directory.
telsa:~ mahmoh$ ls -altrh
total 136
drwxr-xr-x 5 root admin 170B Jun 24 00:27 ../
drwx------+ 4 mahmoh staff 136B Jun 24 00:27 Pictures/
drwx------+ 3 mahmoh staff 102B Jun 24 00:27 Music/
drwx------+ 3 mahmoh staff 102B Jun 24 00:27 Movies/
drwx------+ 3 mahmoh staff 102B Jun 24 00:27 Desktop/
-rw------- 1 mahmoh staff 3B Jun 24 00:27 .CFUserTextEncoding
drwxr-xr-x+ 5 mahmoh staff 170B Jun 24 00:27 Public/
drwx------+ 5 mahmoh staff 170B Jun 24 02:19 Documents/
-rw-r--r--# 1 mahmoh staff 15K Jun 24 02:19 .DS_Store
drwx------# 36 mahmoh staff 1.2K Jun 24 14:48 Library/
-rw-r--r-- 1 mahmoh staff 279B Jun 24 15:27 .profile~
-rw-r--r--# 1 mahmoh staff 14K Jun 24 15:29 .vimrc
-rw-r--r-- 1 mahmoh staff 279B Jun 24 15:30 .profile
drwx------ 2 mahmoh staff 68B Jun 24 15:46 .Trash/
drwxr-xr-x 3 mahmoh staff 102B Jun 24 20:26 .mplayer/
-rw------- 1 mahmoh staff 3.5K Jun 24 22:11 .bash_history
-rw------- 1 mahmoh staff 42B Jun 24 23:25 .lesshst
-rw-r--r-- 1 mahmoh staff 3.6K Jun 24 23:39 temp
-rw-r--r-- 1 mahmoh staff 3.3K Jun 24 23:43 rtorrent.rc~
drwxr-xr-x 5 mahmoh staff 170B Jun 24 23:52 torrents/
-rw-r--r-- 1 mahmoh staff 3.3K Jun 24 23:56 .rtorrent.rc~
-rw------- 1 mahmoh staff 3.7K Jun 24 23:56 .viminfo
-rw-r--r-- 1 mahmoh staff 3.3K Jun 24 23:56 .rtorrent.rc
drwxr-xr-x+ 25 mahmoh staff 850B Jun 24 23:56 ./
drwx------+ 10 mahmoh staff 340B Jun 24 23:58 Downloads/
Now watch.
telsa:~ mahmoh$ ls -altrh| ssed -R -e 's/ +.+ / / g'
total 136
drwxr-xr-x ../
drwx------+ Pictures/
drwx------+ Music/
drwx------+ Movies/
drwx------+ Desktop/
-rw------- .CFUserTextEncoding
drwxr-xr-x+ Public/
drwx------+ Documents/
-rw-r--r--# .DS_Store
drwx------# Library/
-rw-r--r-- .profile~
-rw-r--r--# .vimrc
-rw-r--r-- .profile
drwx------ .Trash/
drwxr-xr-x .mplayer/
-rw------- .bash_history
-rw------- .lesshst
-rw-r--r-- temp
-rw-r--r-- rtorrent.rc~
drwxr-xr-x torrents/
-rw-r--r-- .rtorrent.rc~
-rw------- .viminfo
-rw-r--r-- .rtorrent.rc
drwxr-xr-x+ ./
drwx------+ Downloads/

ls -l | sed 's/^\([^\t ]\+\)\(.*:.[^ \t]\+\)\(.\+\)/\1 \3/'

Here is a working command. The slightly tricky thing is that ls -l will print the year for files that are older than some time (6 months) and hh:mm for newer files.
ls -l | sed 's/ .*[0-9]* .*[A-Z][a-z][a-z] [ 0-9][0-9] \{1,2\}[0-9][0-9]:*[0-9][0-9] / /'
For the following example
drwxr-xr-x 39 root root 1024 Feb 19 08:58 /
the starting .* will match 39 root root 1024 and then the rest of the regular expression matches month name (so you might restrict a-z to fewer characters) followed by year or hh:mm.

why not use awk instead of sed? awk is built for stuff like this.
see this manual page for more about fields in awk.

Like this?
ls -l | sed 's/ [0-9].*:.[0-9] / /' | less
Transforms
-rw-r--r-- 1 tomislav tomislav 609 2009-11-26 10:32 Test.class
-rw-r--r-- 1 tomislav tomislav 46 2009-12-14 12:16 test.groovy
into
-rw-r--r-- Test.class
-rw-r--r-- test.groovy

Related

Why doesn't "ls -al *.swp" show all files with .swp extension in bash shell

Directory content
rtetteh#PW02R9F3:~/Projects$ ls -al
total 68
drwxr-xr-x 5 rtetteh rtetteh 4096 Oct 27 13:45 .
drwxr-xr-x 18 rtetteh rtetteh 4096 Oct 27 13:45 ..
-rw-r--r-- 1 rtetteh rtetteh 12288 Sep 30 15:19 .ThreadTest.cpp.swp
drwxr-xr-x 2 rtetteh rtetteh 4096 Oct 27 13:45 .recycleBin
-rw-r--r-- 1 rtetteh rtetteh 953 Oct 27 13:44 ThreadTest.cpp
-rwxr-xr-x 1 rtetteh rtetteh 24984 Sep 30 15:14 ThreadTest_exe
drwxr-xr-x 2 rtetteh rtetteh 4096 Aug 17 18:26 hello
drwxr-xr-x 6 rtetteh rtetteh 4096 Oct 21 15:12 python-account-manager
-rwxr-xr-x 1 rtetteh rtetteh 168 Aug 19 20:23 test_pos_param.sh
Test 1
rtetteh#PW02R9F3:~/Projects$ ls -al *.sh
-rwxr-xr-x 1 rtetteh rtetteh 168 Aug 19 20:23 test_pos_param.sh
Test 2
rtetteh#PW02R9F3:~/Projects$ ls -al *.swp
ls: cannot access '*.swp': No such file or directory
Why does Test 1 work and not Test 2.
How do I get Test 2 to work i.e show files with .swp extension
Your incorrect assumption is that asterisk expands to include "." at the start of the string. Shell defaults don't make that match in regexp.
You need to specifically specify ".*.swp" in order to make the correct expansion. That also doesn't need the -a switch for ls, because you specified the "." prefix.

How to rename all directories on a terminal

I have a total of 31 directories. Every directory has a random name.
❯ ls -al
total 32
drwxr-xr-x 34 shinokada staff 1088 Dec 28 17:16 .
drwxr-xr-x 32 shinokada staff 1024 Dec 28 17:12 ..
-rw-r--r--# 1 shinokada staff 14340 Dec 28 17:16 .DS_Store
drwxr-xr-x 5 shinokada staff 160 Dec 28 17:10 05618066
drwxr-xr-x 5 shinokada staff 160 Dec 28 17:08 0fef2d20
drwxr-xr-x 5 shinokada staff 160 Dec 28 17:09 11ff096d
drwxr-xr-x 5 shinokada staff 160 Dec 28 17:09 1651ff1f
drwxr-xr-x 5 shinokada staff 160 Dec 28 17:09 2123b256
... and more
Now I'd like to rename them sample1 sample2 sample3 ...sample31.
How can I do it on a terminal(bash/zsh)?
Is there a quick way to do it rather than rename one by one?
This should do:
num=1; for dir in */ ; do mv "${dir}" "sample$num" ; ((num++)); done
I assume you don't want to rename the files, only directories inside your current working directory.

Can I change the modified date, just the year, of a file or all files?

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

Extract lines with duplicate last field

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

Symfony2 : can't duplicate site because of strange folders architecture

I have to retrieve an existing symfony2 (2.1.13) project from prod server in order to run it locally to make some changes. The problem is the architecture seems to be a little bit strange. Please have a look on it :
$pwd
/var/www/html/sites/www.mysite.com
drwxr-xr-x 2 ... 4.0K Jun 30 09:30 backups/
drwxr-x--- 2 ... 6 Mar 15 2013 cgi-bin/
drwxr-xr-x 4 ... 41 Aug 10 18:09 _copie1/
drwxr-xr-x 2 ... 6 Apr 25 2014 log/
drwxr-xr-x 12 ... 4.0K Aug 11 15:41 releases/
drwxr-xr-x 7 ... 146 May 12 16:46 repo/
drwxr-xr-x 5 ... 85 May 12 16:49 shared/
lrwxrwxrwx 1 ... 58 Jul 6 15:11 current -> /var/www/html/sites/www.mysite.com/releases/201508/
lrwxrwxrwx 1 ... 12 Mar 28 2013 html -> current/web//
-rw-r--r-- 1 ... 1022 Jul 6 15:11 revisions.log
current/
drwxr-xr-x 6 ... 4.0K Jul 6 15:09 app/
drwxr-xr-x 2 ... 124 Jul 6 15:05 bin/
drwxr-xr-x 4 ... 50 Aug 11 14:54 src/
drwxr-xr-x 35 ... 4.0K Aug 11 12:08 vendor/
drwxr-xr-x 5 ... 4.0K Aug 11 18:28 web/
-rw-r--r-- 1 ... 626 Jul 6 15:05 Capfile
-rw-r--r-- 1 ... 4.1K Jul 6 15:05 composer.json
-rw-r--r-- 1 ... 167K Jul 6 15:05 composer.lock
-rw-r--r-- 1 ... 216 Jul 6 15:05 Gemfile
-rw-r--r-- 1 ... 1.2K Jul 6 15:05 Gemfile.lock
-rw-r--r-- 1 ... 1.1K Jul 6 15:05 LICENSE
-rw-r--r-- 1 ... 1.6K Jul 6 15:05 README.md
-rw-r--r-- 1 ... 8 Jul 6 15:06 REVISION
-rw-r--r-- 1 ... 7.7K Jul 6 15:05 UPGRADE.md
-rw-r--r-- 1 ... 7 Jul 6 15:05 VERSION
current/web/
drwxr-xr-x 2 ... 4.0K Aug 11 12:10 bundles/
drwxr-xr-x 4 ... 4.0K Jul 6 15:11 compiled/
drwxr-xr-x 2 ... 47 Jul 6 15:11 js/
-rw-r--r-- 1 ... 46K Jul 6 15:11 apc.php
-rw-r--r-- 1 ... 11K Jul 6 15:05 apple-touch-icon.png
-rw-r--r-- 1 ... 7.0K Jul 6 15:05 apple-touch-icon-precomposed.png
-rw-r--r-- 1 ... 728 Jul 6 15:05 app.php
-rw-r--r-- 1 ... 33K Jul 6 15:05 favicon.ico
-rw-r--r-- 1 ... 20K Jul 6 15:05 favicon.jpg
-rw-r--r-- 1 ... 4.6K Jul 6 15:05 favicon.png
lrwxrwxrwx 1 ... 51 Jul 6 15:06 image -> /var/www/html/sites/www.mysite.com/shared/web/image/
lrwxrwxrwx 1 ... 52 Jul 6 15:06 medias -> /var/www/html/sites/www.mysite.com/shared/web/medias/
-rw-r--r-- 1 ... 66 Aug 10 12:28 robots.txt
-rw-r--r-- 1 ... 2.2M Aug 11 03:00 sitemap.xml
current/web/bundles/
lrwxrwxrwx 1 ... 147 Jul 6 15:09 bazingaexposetranslation -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/willdurand/js-translation-bundle/Bazinga/ExposeTranslationBundle/Resources/public/
lrwxrwxrwx 1 ... 121 Jul 6 15:09 ericharddms -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/erichard/dms-bundle/Erichard/DmsBundle/Resources/public/
lrwxrwxrwx 1 ... 126 Jul 6 15:09 farosadmin -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/faros/admin-bundle-legacy/Faros/AdminBundle/Resources/public/
lrwxrwxrwx 1 ... 125 Jul 6 15:09 faroselfinder -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/faros/elfinder-bundle/Faros/ElFinderBundle/Resources/public/
lrwxrwxrwx 1 ... 113 Jul 6 15:09 farosqb -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/faros/qb-bundle/Faros/QBBundle/Resources/public/
lrwxrwxrwx 1 ... 116 Jul 6 15:09 fosjsrouting -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/friendsofsymfony/jsrouting-bundle/Resources/public/
lrwxrwxrwx 1 ... 133 Jul 6 15:09 framework -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/public/
lrwxrwxrwx 1 ... 132 Jul 6 15:09 mopabootstrap -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/mopa/bootstrap-bundle/Mopa/Bundle/BootstrapBundle/Resources/public/
lrwxrwxrwx 1 ... 129 Jul 6 15:09 trsteelckeditor -> /var/www/html/sites/www.mysite.com/releases/201508/vendor/trsteel/ckeditor-bundle/Trsteel/CkeditorBundle/Resources/public/
lrwxrwxrwx 1 ... 99 Jul 6 15:09 mysiteadmin -> /var/www/html/sites/www.mysite.com/releases/201508/src/mysite/AdminBundle/Resources/public/
lrwxrwxrwx 1 ... 98 Jul 6 15:09 mysitesite -> /var/www/html/sites/www.mysite.com/releases/201508/src/mysite/SiteBundle/Resources/public/
current/app/
drwxr-xr-x 4 ... 27 Aug 11 18:32 cache/
drwxr-xr-x 3 ... 4.0K Aug 11 12:12 config/
drwxr-xr-x 2 ... 4.0K Jul 6 15:05 DoctrineMigrations/
drwxr-xr-x 8 ... 105 Jul 6 15:06 Resources/
-rw-r--r-- 1 ... 141 Jul 6 15:05 AppCache.php
-rw-r--r-- 1 ... 4.3K Jul 6 15:05 AppKernel.php
-rw-r--r-- 1 ... 474 Jul 6 15:05 autoload.php
-rw-r--r-- 1 ... 48K Jul 6 15:09 bootstrap.php.cache
-rw-r--r-- 1 ... 1.7K Jul 6 15:09 check.php
-rwxr-xr-x 1 ... 794 Jul 6 15:05 console*
lrwxrwxrwx 1 ... 50 Jul 6 15:06 logs -> /var/www/html/sites/www.mysite.com/shared/app/logs/
-rw-r--r-- 1 ... 1.7K Jul 6 15:05 phpunit.xml.dist
-rw-r--r-- 1 ... 26K Jul 6 15:09 SymfonyRequirements.php
lrwxrwxrwx 1 ... 49 Jul 6 15:06 var -> /var/www/html/sites/www.mysite.com/shared/app/var/
As you can see there are so many symlinks :(
I have a new installation of symfony 2.1.13 on my wamp server, so :
1. What should I exactly copy from the server ?
2. And where to copy ?
3. How to configure files like AppKernel.php, config.php, parameters.yml ...
Thanks :)
Looks like you are stepped to the task from apposite side
First of all "strange" folders architecture is comming from the nature of the deployment tool "capifony". You can see folder "releases" that keeps all history of releases deployed and folder(symlink) "current" that points to the latest(current) release. Also you can see some symlinks to the folder "shared" that suppose to keep some content that doesn't suppose change from deploy to deploy (like images, logs).
It is possible to explain long how capifony works, but seems it is better to get some quick overview http://capifony.org/
In short words I can say that you can copy the site to local, but it is not the best way. I suppose you need to clone the project from its repository(most probably git) and setup parameters.yml
You should not change AppKernel.php or config.php
Clone the respository and looks for app/config/parameters.yml.dist that will(should) have initial configuration for the project
PS: you should not copy "current/web/bundles/" but should use "assets install" command to create these assets
PPS: And I believe you should to learn some symfony docs(at least basic) before start to copy the site

Resources