How to rename all directories on a terminal - bash

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.

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.

Using ls -lR ignore given REGEX

I’m using ls -lR | ... to get my desired output, but now I need something that would force ls to ignore given regex expression. Is it possible to achieve this? I found ls -I and ls --ignore but for some reason I get: illegal option error [macOS Catalina 10.15.3]. I need this to be working on most frequently used shells(bash, ksh, zsh etc..)
Output
ls -lR | ...
./NOEMPTY:
total 72
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 10.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 15.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 20.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 25.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 40.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 5.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b.c
./NOT_THIS:
total 32
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
./THIS_YES:
total 32
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
Desired Output:
ls -lR IGNORE NOT_THIS | ... something more
./NOEMPTY:
total 72
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 10.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 15.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 20.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 25.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 40.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 5.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b.c
./THIS_YES:
total 32
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
I’ve tried to use sed utility but failed to achieve this, since I wasn’t able to select empty line between folders such as this:
ls -lR | sed -n '/.\/NOT_THIS/,/[[:space:]]/p'
Try this : ls -lR -I '*NOT_THIS*'
Demo :
:=>ls -1
1.txt
2.txt
a.csv
b.csv
:=>ls -1 -I '*txt'
a.csv
b.csv
:=>
Using SED
sed '/\.\/NOT_THIS/, /^$/'d
explanation : sed '/start/,/end/d' <-- this will delete all data between first /<patern>/ and second pattern.
^$ <-- Blank line. ^ -- Start of record. $ -- end of record
Demo:
:=>sed '/\.\/NOT_THIS/, /^$/'d file.txt
ls -lR | ...
./NOEMPTY:
total 72
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 10.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 15.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 20.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 25.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 40.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 5.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b.c
./THIS_YES:
total 32
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 30.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35 2.c
-rw-r--r--# 1 makaveli_10 staff 34 Mar 15 09:26 1b copy 35.c
:=>

Sorting ls output by file size

I am currently sorting the output of ls -l by byte count using:
ls -l | sort -r -k5,5 -n
What if I wanted to make this work with the -# flag? Currently this will output:
-rwxr-xr-x# 1 name staff 7106 2 May 10:43 c
-rwxr-xr-x 1 name staff 675 22 Apr 17:57 a
-rwxr-xr-x 1 name staff 486 23 Apr 07:56 b
drwxr-xr-x 4 name staff 136 25 Apr 18:38 d
-rwxr-xr-x 1 name staff 120 23 Apr 07:59 e
-rwxr-xr-x 1 name staff 112 22 Apr 18:45 g
-rwxr-xr-x 1 name staff 51 22 Apr 18:45 f
total 56
com.apple.metadata:_kMDItemUserTags 42
Where I want it to take the extended attribute keys line and keep it below the appropriate file like so:
-rwxr-xr-x# 1 name staff 7106 2 May 10:43 c
com.apple.metadata:_kMDItemUserTags 42
-rwxr-xr-x 1 name staff 675 22 Apr 17:57 a
-rwxr-xr-x 1 name staff 486 23 Apr 07:56 b
drwxr-xr-x 4 name staff 136 25 Apr 18:38 d
-rwxr-xr-x 1 name staff 120 23 Apr 07:59 e
-rwxr-xr-x 1 name staff 112 22 Apr 18:45 g
-rwxr-xr-x 1 name staff 51 22 Apr 18:45 f
total 56
No need to use sort, just use the -S option with ls
ls -Sl
(that's an upper case S)

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

select fields with sed

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

Resources