Articles taggués ‘shell’

How to Bulk Rename Files in Linux (Terminal or GUI)

02/07/2020 Aucun commentaire

Source: Webmaster tips

There are several ways to bulk rename files in Linux. If you want to bulk rename files with a GUI tool, try Thunar file manager, or Krename.

Tip: To install Thunar on Ubuntu/Debian, type sudo apt-get install thunar in a terminal. To install Krename, type sudo apt-get install krename.
How to bulk rename files from the terminal

If you have a directory of files that you would like to bulk rename, you can use the rename command from the terminal.
UPDATE: I believe the Perl-based rename command is only available on Debian-based Linux distros, but there are instructions on adding it to other distros below.

The syntax for the rename command is:

rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

-v means “verbose” and it will output the names of the files when it renames them. It is a good idea to use this feature so you can keep track of what is being renamed. It is also a good idea to do a test run with -n which will do a test run where it won’t rename any files, but will show you a list of files that would be renamed.

The “perlexpr” part of the command is a Perl expression. Don’t panic yet…
The “rename” command in action

Here is an example of the rename command:

rename -n ’s/\.htm$/\.html/’ *.htm

The -n means that it’s a test run and will not actually change any files. It will show you a list of files that would be renamed if you removed the -n. In the case above, it will convert all files in the current directory from a file extension of .htm to .html.

If the output of the above test run looked ok then you could run the final version:

rename -v ’s/\.htm$/\.html/’ *.htm

The -v is optional, but it’s a good idea to include it because it is the only record you will have of changes that were made by the rename command as shown in the sample output below:

$ rename -v 's/\.htm$/\.html/' *.htm
 3.htm renamed as 3.html
 4.htm renamed as 4.html
 5.htm renamed as 5.html

The tricky part in the middle is a Perl substitution with regular expressions, highlighted below:

rename -v ’s/\.htm$/\.html/’ *.htm

Tip: There is an intro to Perl regular expressions here.

Basically the “s” means substitute. The syntax is s/old/new/ — substitute the old with the new.

A . (period) has a special meaning in a regular expression — it means “match any character”. We don’t want to match any character in the example above. It should match only a period. The backslash is a way to “escape” the regular expression meaning of “any character” and just read it as a normal period.

The $ means the end of the string. \.htm$ means that it will match .htm but not .html.

It’s fairly basic — substitute .htm with .html:


The last part of the command, highlighted below, means to apply the rename command to every file that ends with .htm (the * is a wildcard).

rename -v ’s/\.htm$/\.html/’ *.htm

Other Examples

Maybe you have a digital camera that takes photos with filenames something like 00001234.JPG, 00001235.JPG, 00001236.JPG. You could make the .JPG extension lowercase with the following command executed from the same directory as the images:

rename -v 's/\.JPG$/\.jpg/' *.JPG

Here is the output of the above command:

$ rename -v 's/\.JPG$/\.jpg/' *.JPG
 00001111.JPG renamed as 00001111.jpg
 00001112.JPG renamed as 00001112.jpg
 00001113.JPG renamed as 00001113.jpg

That is simple enough, as it is similar to the .html example earlier. You could also bulk rename them with something descriptive at the beginning like this:

Tip: Before trying more complicated renaming like in the example below, do a test run with the -n option as described at the beginning of this tutorial.

rename -v 's/(\d{8})\.JPG$/BeachPics_$1\.jpg/' *.JPG

That will change filenames that have the pattern ########.JPG (8 numbers and capital .JPG) to something like BeachPics_########.jpg (the same 8 numbers and changing the extension to lowercase .jpg). Here is a test run with the -n option:

$ rename -n 's/(\d{8})\.JPG$/BeachPics_$1\.jpg/' *.JPG
 00001111.JPG renamed as BeachPics_00001111.jpg
 00001112.JPG renamed as BeachPics_00001112.jpg
 00001113.JPG renamed as BeachPics_00001113.jpg

Here’s a quick breakdown of the Perl substitution with the regular expression above.

The highlighted section below means to count 8 digits. The parentheses mean to save those 8 digits for later because they are going to be used again in the second half of the substitution:


In the highlighted section below, it adds the string BeachPics, and underscore, and then the text in parentheses from the first half of the substitution. $1 will insert the string from the first set of parentheses that it finds — in this case the 8 digits. If you have more than one set of parentheses you can access the second set with the Perl variable $2 and so on.


Final Refinement

The following variation would make even cleaner-looking filenames. See if you can figure out how it works:

$ rename -n 's/\d{5}(\d{3})\.JPG$/BeachPics_$1\.jpg/' *.JPG
 00000123.JPG renamed as BeachPics_123.jpg
 00000124.JPG renamed as BeachPics_124.jpg
 00000125.JPG renamed as BeachPics_125.jpg

Learning Perl Regular Expressions

You can learn more about Perl regex here and in the Perl regular expression documentation, and the quickstart. Also check out this regex cheatsheet. You can learn more about the rename command by typing man rename in the terminal.
Adding the Rename Command to Non-Debian Distros

The following information was provided by Dan Fego on how to add the Perl-based rename command to non-Debian Linux distros:
I did the following [on Gentoo], though I’m aware there’s both more concise and generic ways to do it. This was just my path of discovery.

[the following commands are from Ubuntu]

# which rename
 # ls -l /usr/bin/rename
 lrwxrwxrwx 1 root root 24 Dec 2 02:13 /usr/bin/rename ->
 # ls -l /etc/alternatives/rename
 lrwxrwxrwx 1 root root 16 Dec 6 00:29 /etc/alternatives/rename ->
 # ls -l /usr/bin/prename
 -rwxr-xr-x 1 root root 2987 Dec 4 04:18 /usr/bin/prename

Once I had the final link in the chain [on Ubuntu], I knew which file to copy [to Gentoo], though really I didn’t need to follow it all that way. The reason I did to begin with was because I tried to copy it with the filesystem mounted, and it referred to absolute paths which didn’t exist outside of the Ubuntu environment. The prename script is fully self-contained, assuming you’ve got Perl on your system (which just about any self-respecting Linux distro does).

The code from Ubuntu’s rename/prename is here (Gutsy Gibbon).

Categories: Système Tags: ,

OS X: How to reset the DNS cache

29/06/2020 Aucun commentaire

OS X Mountain Lion or Lion

Use the following Terminal command to reset the DNS cache:

sudo killall -HUP mDNSResponder

Mac OS X v10.6

Use the following Terminal command to reset the DNS cache:

sudo dscacheutil -flushcache
Categories: Réseau Tags: ,

Best way to backup all settings, list of installed packages, tweaks, etc?

28/06/2020 Aucun commentaire



A quick way of backing up a list of programs is to run this:

dpkg --get-selections > ~/Package.list
sudo cp /etc/apt/sources.list ~/sources.list
sudo apt-key exportall > ~/Repo.keys


It will back them up in a format that dpkg can read for after your reinstall, like this:

sudo apt-key add ~/Repo.keys
sudo cp ~/sources.list /etc/apt/sources.list 
sudo apt-get update
sudo apt-get install dselect
sudo dpkg --set-selections < ~/Package.list
sudo apt-get dselect-upgrade -y


Settings and Personal Data


Before you reinstall, you should probably back up the settings from some of your programs, this can easily be done by grabbing folders from /etc and all the content from your user directory (not just the stuff you can see in nautilus!):

rsync --progress /home/`whoami` /path/to/user/profile/backup/here


After you reinstall, you can restore it with:

rsync --progress /path/to/user/profile/backup/here /home/`whoami`


So all together as a pseudo-bash script.


This assumes there is only one user on the machine (remove /'whoami' otherwise) and that you used the same username on both installs (modify dest. of rsync otherwise).

dpkg --get-selections > ~/Package.list
sudo cp /etc/apt/sources.list ~/sources.list
sudo apt-key exportall > ~/Repo.keys
rsync --progress /home/`whoami` /path/to/user/profile/backup/here

##  Reinstall now

rsync --progress /path/to/user/profile/backup/here /home/`whoami`
sudo apt-key add ~/Repo.keys
sudo cp ~/sources.list /etc/apt/sources.list 
sudo apt-get update
sudo apt-get install dselect
sudo dpkg --set-selections < ~/Package.list
sudo dselect


Merging directories (folders) on Mac OS X

27/06/2020 Comments off

merging directoriesEvery now and then I find myself in a situation where I have a folder (I’ll call it source) of files and nested folders, possibly many levels deep, that I want to copy into another folder (which I’ll call target). target already contains some of the files and folders I’m copying, and it also has files and folders that are not present in source.

Simply copying source to target’s parent folder in the Mac OS X Finder will replace everything in target with the contents of source. This is not always what I want, and in my opinion it’s one of the biggest flaws of the Mac OS X Finder. Not just Mac OS X actually – back in the pre-Mac OS X days there was a utility called Speed Doubler that patched the Finder to add a smart replace option when copying files.

It’s possible to manually open each folder and their subfolders and copy just the files, but it can be very tedious. There are also third party software options that let you merge files when copying, and if you have Apple’s Developer Tools installed there is the FileMerge utility.

However, you can open a Terminal window and copy the files from the command line, which saves you from installing extra software. Since I keep looking up the syntax every time I need to do this I decided to document it here for future reference.


One command line utility that can copy directories without replacing everything in them is cp:

cp -pRv source/ target

The pRv options do the following:

  • p preserves timestamps, flags, modes, and ownerships of files
  • R copies the entire subtree
  • v makes cp output the name of each file that is copied

Note: The / after the name of the source directory is important since it tells cp to copy the contents of the directory and not the directory itself.


You can also use rsync:

rsync -av source/ target

The av options do this:

  • a tells rsync to copy recursively and preserve file attributes
  • v makes rsync print information to the terminal window about what was copied

Just as with the cp command, the trailing slash after the source directory is important to make sure only the contents of the directory are copied.


A third option is ditto:

ditto -V source target

The V option prints information about what was copied.

Deleting files that don’t exist in the source directory

Sometimes you want a merge to delete files that exist in the target directory but not in the source directory. That’s easy with rsync (but be careful as there is no undo):

rsync -av --delete source/ target

But wait! What if the target is under version control? Won’t that delete any .svn or .git directories as well? Yep. That can be avoided by adding filters. Let’s say you want to keep all .htaccess and .svn files or directories in the target directory. This does just that:

rsync -av --delete --filter="- .htaccess" --filter="- .svn" source/ target

A useful tip when you involve delete is to add the n option at first to do a “dry run” and only show what would have been deleted. Again, be careful with delete since there is no undo.

Hoping for Finder integration

It would be great if Apple could make it possible to use the Finder to copy folders like this. It could be a secret option somewhere or invoked when you hold certain modifier keys when copying. It doesn’t matter as long as it’s possible.

Most people probably don’t need this feature every day, but when you do need it it can save lots of time. Having the feature built into the Finder would also reduce the risk of people accidentally deleting files because they don’t realise copying folders replaces everything inside them.

Categories: Logiciel, Système Tags: , , ,

Accéder à un serveur ssh comme si c’était un répertoire local

23/06/2020 Comments off

1. Installez sshfs:

sudo aptitude install sshfs

2. Créez le répertoire où apparaîtra le serveur ssh:

mkdir pointdemontage

3. Ajoutez les utilisateurs autorisés à utiliser le logiciel au groupe fuse :

sudo adduser username fuse

4. Montez le serveur ssh:

sshfs login@monserveurssh: pointdemontage

(N’oubliez pas les deux points (:) après le nom ou l’adresse du serveur ssh)

Et voilà !

Vous pouvez accéder au contenu de votre serveur ssh comme si c’était un simple répertoire local !

Pour démonter l’accès:

fusermount -u pointdemontage

Pour plus d’informations, voir la documentation ubuntu :