Showing posts with label at once. Show all posts
Showing posts with label at once. Show all posts

04 February 2023

How to rename multiple file names all at once?

https://ostechnix.com/how-to-rename-multiple-files-at-once-in-linux/


Method 4 - Rename multiple files at once using vimv

As the name says, Vimv is a command line utility to bulk rename files using Vim editor. You can, of course, change the editor by changing the value of $EDITOR environment variable.

To install Vimv, git clone the repository:

$ git clone https://github.com/thameera/vimv.git

Copy the vimv binary to your $PATH, for example /usr/local/bin/.

$ sudo cp vimv/vimv /usr/local/bin/

Finally, make it executable:

$ sudo chmod +x /usr/local/bin/vimv

Now go to the directory and run the following command to edit the filenames.

$ vimv

You will see the filenames in Vi editor. Press i to switch to interactive mode and edit the filenames as the way you edit text in Vi editor. Once done, press ESC key and type :wq to save and exit.

The files inside the directory should be renamed now.

 

Method 3 - Rename files using renameutils

The renameutils is a set of programs that is designed to batch renaming files and directories faster and easier.

Renameutils consists of the following five programs:

  1. qmv (quick move),
  2. qcp (quick copy),
  3. imv (interactive move),
  4. icp (interactive copy),
  5. deurlname (delete URL).

Install renameutils in Linux

Renameutils is available in the default repositories of most Linux distributions. To install it on Arch-based systems, enable the community repository and run:

$ sudo pacman -Syu renameutils

On Debian-based systems:

$ sudo apt install renameutils

Now, let us see some examples.

1. qmv

The qmv program will open the filenames in a directory in your default text editor and allows you to edit them.

I have the following three files in a directory named 'ostechnix'.

$ ls ostechnix/
abcd1.txt abcd2.txt abcd3.txt

To rename the filenames in the 'ostechnix' directory, simply do:

$ qmv ostechnix/

Now, change the filenames as you wish. You will see the live preview as you edit the filenames.

Alternatively, you can cd into the directory and simply run 'qmv'.

Once you opened the files, you will see the two columns as shown in the following screenshot.

Bulk rename files using qmv
Bulk rename files using qmv

The left column side displays the source filenames and the right column displays the destination names (the output filenames that you will get after editing).

Now, rename all the output names on the right side as you wish.

Bulk rename files using qmv
Bulk rename files using qmv

After renaming filenames, save and quit the file.

Finally, you will see the following output:

Plan is valid.

abcd1.txt -> xyzd1.txt
abcd2.txt -> xyzd2.txt
abcd3.txt -> xyzd3.txt
   Regular rename

abcd1.txt -> xyzd1.txt
abcd2.txt -> xyzd2.txt
abcd3.txt -> xyzd3.txt

Now, check if the changes have actually been made using 'ls' command:

$ ls ostechnix/
xyzd1.txt xyzd2.txt xyzd3.txt

See? All files are renamed. Not just files, the renameutils will also rename the directory names as well.

Here is a quick video demo of qmv program:

Bulk rename files using qmv
Bulk rename files using qmv

If you don't want to edit the filenames in dual-column format, use the following command to display the destination file column only.

$ qmv -f do ostechnix/

Where, '-f' refers the format and 'do' refers destination-only.

Now, you will see only the destination column. That's the column we make the changes.

Display the destination file column only in qmv
Display the destination file column only in qmv

Once done, save and close the file.

For more details, refer man pages.

$ man qmv

 

01 January 2023

How to do substitution in all files at once in vim?

Vim search replace all files in current (project) folder

e.g. to substitute 'bar' for 'foo' in all Python files in current directory

Within vim, run the commands below,

:arg *.py
:argdo %s/foo/bar/gce | update


:arg *.py means we will do substitution in all Python files in current
directory

'update' means to write a file only if changes have been made.


26 January 2022

How to break all lines into 78 character long all at once in vim?

(Author: Sherman Chen)


Method 1
========

:set textwidth=78

gggqG

"gg" takes you to the first line, "gq" is the format operator and "G" the motion that jumps to the last line.


Method 2
========

:set textwidth=78 

:1,$g/.*/normal jgwap



Method 3
========

1. :set linebreak
   to recover those broken words at the end of each line into whole words.

2. :set textwidth=78

3. Find a line which is longer than 78 characters, say line #3.  Move the cursor to that line minus one, i.e. 3 - 1 = 2.

4. qaj$aa Backspace Escape q

5. See how many lines the file contains totally, say, 100.

6. gg

7. 100@a Enter

All the lines will become 78 character long now.


Method 4 (which has improved Method
3)
========

1. :set linebreak
   to recover those broken words at the end of each line into whole words.

2. :set textwidth=78

3. Move the cursor to any line which is longer than 78 characters.

4. qa$aa Backspace Escape q

5. :%normal @a

All the lines will become 78 character long now.


(END)