14.0k views
1 vote
Organize a list of files by their extension into new folders in the current directory using bash

a) Use the "mv" command to move files into separate folders based on their extensions.
b) Utilize the "ls" command to group files by their extensions.
c) Create a new folder for each file extension and manually sort the files.
d) Utilize the "find" command to search for files by their extensions and move them.

1 Answer

0 votes

Final answer:

The question involves using Bash scripting to organize files by their extensions into new folders. This can be done by listing the files, creating directories for each extension, and moving the files accordingly using a combination of ls, mkdir, and mv commands.

Step-by-step explanation:

The question concerns the use of Bash scripting to organize a list of files by their extension into new folders within the current directory. The correct approach would involve writing a script or using command-line operations to:

  1. List all files in the directory using the ls or find command.
  2. Identify the unique file extensions.
  3. Create new folders for each extension using mkdir command.
  4. Move files into the corresponding folders based on their extension using the mv command.

An example command sequence to achieve this would be:

for ext in $(ls *.* | rev | cut -d. -f1 | rev | uniq); do mkdir -p $ext; mv *.$ext $ext/; done

This loop iterates through the list of file extensions, creates a new directory for each one, and moves the files with that extension into the corresponding directory.

User Majed Badawi
by
8.1k points