Mv several file to directory with same basenameChange to the last modified subdirectory of the current directoryDifference between files in directorycannot move Directory not emptyTemporary directory per processHow to move files from certain directories to newly created directories based on their extension?Move file only if another file with different suffix existsFind latest folder/file version and remove - no time stamp dataNeed to loop through folder and move files to different directory?Recursively rename all the files without changing their extensions?How to rename a file to have the same name and extension as another file in same directory
Fencing style for blades that can attack from a distance
How do I create uniquely male characters?
Email Account under attack (really) - anything I can do?
Test whether all array elements are factors of a number
Can I ask the recruiters in my resume to put the reason why I am rejected?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
Did Shadowfax go to Valinor?
What typically incentivizes a professor to change jobs to a lower ranking university?
LaTeX closing $ signs makes cursor jump
What is the word for reserving something for yourself before others do?
What defenses are there against being summoned by the Gate spell?
In Japanese, what’s the difference between “Tonari ni” (となりに) and “Tsugi” (つぎ)? When would you use one over the other?
Languages that we cannot (dis)prove to be Context-Free
How to format long polynomial?
Dragon forelimb placement
Today is the Center
Is it legal for company to use my work email to pretend I still work there?
Why Is Death Allowed In the Matrix?
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Why dont electromagnetic waves interact with each other?
"You are your self first supporter", a more proper way to say it
Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.
Is it possible to do 50 km distance without any previous training?
TGV timetables / schedules?
Mv several file to directory with same basename
Change to the last modified subdirectory of the current directoryDifference between files in directorycannot move Directory not emptyTemporary directory per processHow to move files from certain directories to newly created directories based on their extension?Move file only if another file with different suffix existsFind latest folder/file version and remove - no time stamp dataNeed to loop through folder and move files to different directory?Recursively rename all the files without changing their extensions?How to rename a file to have the same name and extension as another file in same directory
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have several files with different extensions and directories with the same basename, like this example.
FileA.txt
FileA.html
FileA.directory/
FileB.txt
FileB.html
FileB.directory/
FileC.txt
FileC.html
FileC.directory/
And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following
FileA.directory/FileA.txt
FileA.directory/FileA.html
FileB.directory/FileB.txt
FileB.directory/FileB.html
FileC.directory/FileC.txt
FileC.directory/FileC.html
I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.
for dir in .*/
do
for f in "$dir"*
do
base=$f#$dir
mv "$base.*" "$dir"
done
done
However I can't get it to work.
bash directory mv
add a comment |
I have several files with different extensions and directories with the same basename, like this example.
FileA.txt
FileA.html
FileA.directory/
FileB.txt
FileB.html
FileB.directory/
FileC.txt
FileC.html
FileC.directory/
And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following
FileA.directory/FileA.txt
FileA.directory/FileA.html
FileB.directory/FileB.txt
FileB.directory/FileB.html
FileC.directory/FileC.txt
FileC.directory/FileC.html
I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.
for dir in .*/
do
for f in "$dir"*
do
base=$f#$dir
mv "$base.*" "$dir"
done
done
However I can't get it to work.
bash directory mv
add a comment |
I have several files with different extensions and directories with the same basename, like this example.
FileA.txt
FileA.html
FileA.directory/
FileB.txt
FileB.html
FileB.directory/
FileC.txt
FileC.html
FileC.directory/
And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following
FileA.directory/FileA.txt
FileA.directory/FileA.html
FileB.directory/FileB.txt
FileB.directory/FileB.html
FileC.directory/FileC.txt
FileC.directory/FileC.html
I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.
for dir in .*/
do
for f in "$dir"*
do
base=$f#$dir
mv "$base.*" "$dir"
done
done
However I can't get it to work.
bash directory mv
I have several files with different extensions and directories with the same basename, like this example.
FileA.txt
FileA.html
FileA.directory/
FileB.txt
FileB.html
FileB.directory/
FileC.txt
FileC.html
FileC.directory/
And i would like to create a for-loop for which i can move all files to their corresponding directory based on their basename. Resulting in the following
FileA.directory/FileA.txt
FileA.directory/FileA.html
FileB.directory/FileB.txt
FileB.directory/FileB.html
FileC.directory/FileC.txt
FileC.directory/FileC.html
I've tried to find several suggestion which are kind of similar to my problem on this page, like this example. But i can't find a completely similar question.
for dir in .*/
do
for f in "$dir"*
do
base=$f#$dir
mv "$base.*" "$dir"
done
done
However I can't get it to work.
bash directory mv
bash directory mv
edited 2 days ago
Rui F Ribeiro
41.9k1483142
41.9k1483142
asked Mar 11 at 11:06
RAHenriksenRAHenriksen
6
6
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:
topdir=.
for dirname in "$topdir"/*/; do
prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
prefix=$prefix%%.* # FileC.directory --> FileC
for filename in "$topdir/$prefix".*; do
if [ ! -d "$filename" ]; then
mv -i "$filename" "$dirname"
fi
done
done
The outer loop iterates over all directories in the directory $topdir
(here set to .
, the current directory). The $prefix
will be the base name of the directory name, with the bit after the first dot removed.
Once the prefix has been computed, non-directories (files) in the same $topdir
directory that share the same prefix are moved to the directory.
add a comment |
dirs=$(ls | grep ".d")
for d in $dirs
do
filename=$(echo $d | awk -F"." 'print $1')
ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
done
Maybe too verbose, but it works
1
Note that.d
also matches strings likebad
since.
matches any character.
– Kusalananda♦
Mar 11 at 11:52
1
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
@Prvt_Yadv It's not enough. That may still matchFileA.draft.txt
, even though the given list in the question does not contain such a filename.
– Kusalananda♦
Mar 11 at 12:18
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
add a comment |
Assuming we know that all files have the filename suffixes .html
or .txt
and that all directories have the suffix .directory
, you can use:
for i in *.txt *.html
do
mv "$i" "$i%%.*.directory/$i"
done
Run this in same directory where you are having files.
It will remove trailing .txt and .html from file names and then will move files to destination directory.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505607%2fmv-several-file-to-directory-with-same-basename%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:
topdir=.
for dirname in "$topdir"/*/; do
prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
prefix=$prefix%%.* # FileC.directory --> FileC
for filename in "$topdir/$prefix".*; do
if [ ! -d "$filename" ]; then
mv -i "$filename" "$dirname"
fi
done
done
The outer loop iterates over all directories in the directory $topdir
(here set to .
, the current directory). The $prefix
will be the base name of the directory name, with the bit after the first dot removed.
Once the prefix has been computed, non-directories (files) in the same $topdir
directory that share the same prefix are moved to the directory.
add a comment |
Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:
topdir=.
for dirname in "$topdir"/*/; do
prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
prefix=$prefix%%.* # FileC.directory --> FileC
for filename in "$topdir/$prefix".*; do
if [ ! -d "$filename" ]; then
mv -i "$filename" "$dirname"
fi
done
done
The outer loop iterates over all directories in the directory $topdir
(here set to .
, the current directory). The $prefix
will be the base name of the directory name, with the bit after the first dot removed.
Once the prefix has been computed, non-directories (files) in the same $topdir
directory that share the same prefix are moved to the directory.
add a comment |
Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:
topdir=.
for dirname in "$topdir"/*/; do
prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
prefix=$prefix%%.* # FileC.directory --> FileC
for filename in "$topdir/$prefix".*; do
if [ ! -d "$filename" ]; then
mv -i "$filename" "$dirname"
fi
done
done
The outer loop iterates over all directories in the directory $topdir
(here set to .
, the current directory). The $prefix
will be the base name of the directory name, with the bit after the first dot removed.
Once the prefix has been computed, non-directories (files) in the same $topdir
directory that share the same prefix are moved to the directory.
Assuming the names of the files and directories follow the same naming convention in that they share some common grouping prefix followed by a dot, and assuming we don't know the filename suffixes of files or directories:
topdir=.
for dirname in "$topdir"/*/; do
prefix=$( basename "$dirname" ) # $topdir/FileC.directory/ --> FileC.directory
prefix=$prefix%%.* # FileC.directory --> FileC
for filename in "$topdir/$prefix".*; do
if [ ! -d "$filename" ]; then
mv -i "$filename" "$dirname"
fi
done
done
The outer loop iterates over all directories in the directory $topdir
(here set to .
, the current directory). The $prefix
will be the base name of the directory name, with the bit after the first dot removed.
Once the prefix has been computed, non-directories (files) in the same $topdir
directory that share the same prefix are moved to the directory.
edited Mar 11 at 12:03
answered Mar 11 at 11:46
Kusalananda♦Kusalananda
140k17261435
140k17261435
add a comment |
add a comment |
dirs=$(ls | grep ".d")
for d in $dirs
do
filename=$(echo $d | awk -F"." 'print $1')
ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
done
Maybe too verbose, but it works
1
Note that.d
also matches strings likebad
since.
matches any character.
– Kusalananda♦
Mar 11 at 11:52
1
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
@Prvt_Yadv It's not enough. That may still matchFileA.draft.txt
, even though the given list in the question does not contain such a filename.
– Kusalananda♦
Mar 11 at 12:18
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
add a comment |
dirs=$(ls | grep ".d")
for d in $dirs
do
filename=$(echo $d | awk -F"." 'print $1')
ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
done
Maybe too verbose, but it works
1
Note that.d
also matches strings likebad
since.
matches any character.
– Kusalananda♦
Mar 11 at 11:52
1
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
@Prvt_Yadv It's not enough. That may still matchFileA.draft.txt
, even though the given list in the question does not contain such a filename.
– Kusalananda♦
Mar 11 at 12:18
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
add a comment |
dirs=$(ls | grep ".d")
for d in $dirs
do
filename=$(echo $d | awk -F"." 'print $1')
ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
done
Maybe too verbose, but it works
dirs=$(ls | grep ".d")
for d in $dirs
do
filename=$(echo $d | awk -F"." 'print $1')
ls | grep -P "$filename.(?!(d))" | xargs -I mv '' '$d'
done
Maybe too verbose, but it works
answered Mar 11 at 11:45
dgandgan
13919
13919
1
Note that.d
also matches strings likebad
since.
matches any character.
– Kusalananda♦
Mar 11 at 11:52
1
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
@Prvt_Yadv It's not enough. That may still matchFileA.draft.txt
, even though the given list in the question does not contain such a filename.
– Kusalananda♦
Mar 11 at 12:18
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
add a comment |
1
Note that.d
also matches strings likebad
since.
matches any character.
– Kusalananda♦
Mar 11 at 11:52
1
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
@Prvt_Yadv It's not enough. That may still matchFileA.draft.txt
, even though the given list in the question does not contain such a filename.
– Kusalananda♦
Mar 11 at 12:18
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
1
1
Note that
.d
also matches strings like bad
since .
matches any character.– Kusalananda♦
Mar 11 at 11:52
Note that
.d
also matches strings like bad
since .
matches any character.– Kusalananda♦
Mar 11 at 11:52
1
1
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
You should escape it "."
– Prvt_Yadv
Mar 11 at 11:53
@Prvt_Yadv It's not enough. That may still match
FileA.draft.txt
, even though the given list in the question does not contain such a filename.– Kusalananda♦
Mar 11 at 12:18
@Prvt_Yadv It's not enough. That may still match
FileA.draft.txt
, even though the given list in the question does not contain such a filename.– Kusalananda♦
Mar 11 at 12:18
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
Related: Why *not* parse `ls` (and what do to instead)?
– Kusalananda♦
Mar 11 at 12:49
add a comment |
Assuming we know that all files have the filename suffixes .html
or .txt
and that all directories have the suffix .directory
, you can use:
for i in *.txt *.html
do
mv "$i" "$i%%.*.directory/$i"
done
Run this in same directory where you are having files.
It will remove trailing .txt and .html from file names and then will move files to destination directory.
add a comment |
Assuming we know that all files have the filename suffixes .html
or .txt
and that all directories have the suffix .directory
, you can use:
for i in *.txt *.html
do
mv "$i" "$i%%.*.directory/$i"
done
Run this in same directory where you are having files.
It will remove trailing .txt and .html from file names and then will move files to destination directory.
add a comment |
Assuming we know that all files have the filename suffixes .html
or .txt
and that all directories have the suffix .directory
, you can use:
for i in *.txt *.html
do
mv "$i" "$i%%.*.directory/$i"
done
Run this in same directory where you are having files.
It will remove trailing .txt and .html from file names and then will move files to destination directory.
Assuming we know that all files have the filename suffixes .html
or .txt
and that all directories have the suffix .directory
, you can use:
for i in *.txt *.html
do
mv "$i" "$i%%.*.directory/$i"
done
Run this in same directory where you are having files.
It will remove trailing .txt and .html from file names and then will move files to destination directory.
edited Mar 11 at 12:22
Kusalananda♦
140k17261435
140k17261435
answered Mar 11 at 11:41
Prvt_YadvPrvt_Yadv
3,17131330
3,17131330
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505607%2fmv-several-file-to-directory-with-same-basename%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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