Name a series of directories after file names












0















I have a list of files named EX5_##.bak. I want to each one in a directory named EX5_##.

Example



EX5_01.bak
EX5_02.bak
EX5_03.bak


and I want to put them in directories. So when I typy



ls -l


I get :



EX5_01
EX5_02
EX5_03


and so forth where those are directory names and the files of the same name are in the directory. How do I go about this? Is there a single command or Bash script that I can write to achieve this?










share|improve this question























  • You want to what? “I want to each one in a directory …”.

    – ctrl-alt-delor
    Feb 16 at 19:51
















0















I have a list of files named EX5_##.bak. I want to each one in a directory named EX5_##.

Example



EX5_01.bak
EX5_02.bak
EX5_03.bak


and I want to put them in directories. So when I typy



ls -l


I get :



EX5_01
EX5_02
EX5_03


and so forth where those are directory names and the files of the same name are in the directory. How do I go about this? Is there a single command or Bash script that I can write to achieve this?










share|improve this question























  • You want to what? “I want to each one in a directory …”.

    – ctrl-alt-delor
    Feb 16 at 19:51














0












0








0








I have a list of files named EX5_##.bak. I want to each one in a directory named EX5_##.

Example



EX5_01.bak
EX5_02.bak
EX5_03.bak


and I want to put them in directories. So when I typy



ls -l


I get :



EX5_01
EX5_02
EX5_03


and so forth where those are directory names and the files of the same name are in the directory. How do I go about this? Is there a single command or Bash script that I can write to achieve this?










share|improve this question














I have a list of files named EX5_##.bak. I want to each one in a directory named EX5_##.

Example



EX5_01.bak
EX5_02.bak
EX5_03.bak


and I want to put them in directories. So when I typy



ls -l


I get :



EX5_01
EX5_02
EX5_03


and so forth where those are directory names and the files of the same name are in the directory. How do I go about this? Is there a single command or Bash script that I can write to achieve this?







shell-script command-line






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 16 at 19:41









JayJay

54




54













  • You want to what? “I want to each one in a directory …”.

    – ctrl-alt-delor
    Feb 16 at 19:51



















  • You want to what? “I want to each one in a directory …”.

    – ctrl-alt-delor
    Feb 16 at 19:51

















You want to what? “I want to each one in a directory …”.

– ctrl-alt-delor
Feb 16 at 19:51





You want to what? “I want to each one in a directory …”.

– ctrl-alt-delor
Feb 16 at 19:51










1 Answer
1






active

oldest

votes


















7














A simple shell loop:



#!/bin/sh

for file in ./EX5_??.bak; do
dir=${file%.bak}
mkdir -p "$dir" && mv -i "$file" "$dir"
done


This would iterate over all your EX5_??.bak files in the current directory (? matches a single character). For each file, it creates a directory name by stripping the .bak suffix off from the filename (this is what ${file%.bak} does). It then creates the directory if it did not already exist and, if there was no issue with creating the directory, moves the file over into it.



If you need to be more precise with the selection of files, you may want to use ./EX5_[0-9][0-9].bak as the pattern to iterate over. This could be useful if you also have files like EX5_AA.bak that you don't want to include in the loop.



The -p option to mkdir makes the utility not treat it as an error that the directory already exists (it also makes it create intermediate directories, but that's not really used in this instance).



The -i option to mv makes it ask for confirmation before overwriting any files in the target directory. We use it here as a safety catch.






share|improve this answer


























  • Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

    – Jay
    Feb 16 at 20:47













  • @Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

    – Kusalananda
    Feb 16 at 20:49






  • 1





    @Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

    – Kusalananda
    Feb 16 at 20:54






  • 1





    As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

    – Jay
    Feb 16 at 20:57











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501084%2fname-a-series-of-directories-after-file-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














A simple shell loop:



#!/bin/sh

for file in ./EX5_??.bak; do
dir=${file%.bak}
mkdir -p "$dir" && mv -i "$file" "$dir"
done


This would iterate over all your EX5_??.bak files in the current directory (? matches a single character). For each file, it creates a directory name by stripping the .bak suffix off from the filename (this is what ${file%.bak} does). It then creates the directory if it did not already exist and, if there was no issue with creating the directory, moves the file over into it.



If you need to be more precise with the selection of files, you may want to use ./EX5_[0-9][0-9].bak as the pattern to iterate over. This could be useful if you also have files like EX5_AA.bak that you don't want to include in the loop.



The -p option to mkdir makes the utility not treat it as an error that the directory already exists (it also makes it create intermediate directories, but that's not really used in this instance).



The -i option to mv makes it ask for confirmation before overwriting any files in the target directory. We use it here as a safety catch.






share|improve this answer


























  • Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

    – Jay
    Feb 16 at 20:47













  • @Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

    – Kusalananda
    Feb 16 at 20:49






  • 1





    @Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

    – Kusalananda
    Feb 16 at 20:54






  • 1





    As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

    – Jay
    Feb 16 at 20:57
















7














A simple shell loop:



#!/bin/sh

for file in ./EX5_??.bak; do
dir=${file%.bak}
mkdir -p "$dir" && mv -i "$file" "$dir"
done


This would iterate over all your EX5_??.bak files in the current directory (? matches a single character). For each file, it creates a directory name by stripping the .bak suffix off from the filename (this is what ${file%.bak} does). It then creates the directory if it did not already exist and, if there was no issue with creating the directory, moves the file over into it.



If you need to be more precise with the selection of files, you may want to use ./EX5_[0-9][0-9].bak as the pattern to iterate over. This could be useful if you also have files like EX5_AA.bak that you don't want to include in the loop.



The -p option to mkdir makes the utility not treat it as an error that the directory already exists (it also makes it create intermediate directories, but that's not really used in this instance).



The -i option to mv makes it ask for confirmation before overwriting any files in the target directory. We use it here as a safety catch.






share|improve this answer


























  • Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

    – Jay
    Feb 16 at 20:47













  • @Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

    – Kusalananda
    Feb 16 at 20:49






  • 1





    @Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

    – Kusalananda
    Feb 16 at 20:54






  • 1





    As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

    – Jay
    Feb 16 at 20:57














7












7








7







A simple shell loop:



#!/bin/sh

for file in ./EX5_??.bak; do
dir=${file%.bak}
mkdir -p "$dir" && mv -i "$file" "$dir"
done


This would iterate over all your EX5_??.bak files in the current directory (? matches a single character). For each file, it creates a directory name by stripping the .bak suffix off from the filename (this is what ${file%.bak} does). It then creates the directory if it did not already exist and, if there was no issue with creating the directory, moves the file over into it.



If you need to be more precise with the selection of files, you may want to use ./EX5_[0-9][0-9].bak as the pattern to iterate over. This could be useful if you also have files like EX5_AA.bak that you don't want to include in the loop.



The -p option to mkdir makes the utility not treat it as an error that the directory already exists (it also makes it create intermediate directories, but that's not really used in this instance).



The -i option to mv makes it ask for confirmation before overwriting any files in the target directory. We use it here as a safety catch.






share|improve this answer















A simple shell loop:



#!/bin/sh

for file in ./EX5_??.bak; do
dir=${file%.bak}
mkdir -p "$dir" && mv -i "$file" "$dir"
done


This would iterate over all your EX5_??.bak files in the current directory (? matches a single character). For each file, it creates a directory name by stripping the .bak suffix off from the filename (this is what ${file%.bak} does). It then creates the directory if it did not already exist and, if there was no issue with creating the directory, moves the file over into it.



If you need to be more precise with the selection of files, you may want to use ./EX5_[0-9][0-9].bak as the pattern to iterate over. This could be useful if you also have files like EX5_AA.bak that you don't want to include in the loop.



The -p option to mkdir makes the utility not treat it as an error that the directory already exists (it also makes it create intermediate directories, but that's not really used in this instance).



The -i option to mv makes it ask for confirmation before overwriting any files in the target directory. We use it here as a safety catch.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 16 at 20:43

























answered Feb 16 at 19:51









KusalanandaKusalananda

133k17254417




133k17254417













  • Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

    – Jay
    Feb 16 at 20:47













  • @Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

    – Kusalananda
    Feb 16 at 20:49






  • 1





    @Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

    – Kusalananda
    Feb 16 at 20:54






  • 1





    As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

    – Jay
    Feb 16 at 20:57



















  • Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

    – Jay
    Feb 16 at 20:47













  • @Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

    – Kusalananda
    Feb 16 at 20:49






  • 1





    @Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

    – Kusalananda
    Feb 16 at 20:54






  • 1





    As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

    – Jay
    Feb 16 at 20:57

















Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

– Jay
Feb 16 at 20:47







Thank you. This will be helpful not just for this application but gives me a frame work for other things as well. Can you run this from the command line or do I have to make it a script?

– Jay
Feb 16 at 20:47















@Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

– Kusalananda
Feb 16 at 20:49





@Jay You could run this on the command line, but it would be more suited as a shell script. If you just copy and paste it into a terminal, it would execute, likewise if you just typed it in, line for line. It would be safer to write it in a shell script though, as you would be able to read it a few times to make sure that you got everything correctly.

– Kusalananda
Feb 16 at 20:49




1




1





@Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

– Kusalananda
Feb 16 at 20:54





@Jay Also, note that you should never use code that you don't understand, no matter where it comes from.

– Kusalananda
Feb 16 at 20:54




1




1





As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

– Jay
Feb 16 at 20:57





As I always do...I go line by line and understand what each command does. While relatively new to Linux and the command line I do understand what some of this does. Thanks for your explanation and time.

– Jay
Feb 16 at 20:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501084%2fname-a-series-of-directories-after-file-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀