Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7420043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:10:17+00:00 2026-05-29T08:10:17+00:00

I have a directory with sub-directories and files with names that start with a

  • 0

I have a directory with sub-directories and files with names that start with a string similar to the sub-directories; e.g.

 bar/ 
     foo-1/ (dir)
     foo-1-001.txt
     foo-1-002.txt
     foo-1-003.txt
     foo-2/ (dir)
     foo-2-001.txt
     foo-2-002.txt
     foo-2-003.txt
     foo-3/ (dir)
     foo-3-001.txt
     foo-3-002.txt
     foo-3-003.txt  

etc.
All files are currently at the same level. I’d like to move the corresponding .txt files into their similarly-named directories with a script (there are > 9500 in my current situation).

I’ve written the following, but I’m missing something, as I can’t get the files to move.

#!/bin/sh

# directory basename processing for derivatives
# create directory list in a text file
find ./ -type d > directoryList.txt


# setup while loop for moving text files around
FILE="directoryList.txt"
exec 3<&0
exec 0<$FILE
while read line
do
    echo "This is a directory:`basename $line`" 
filemoves=`find ./ -type f -name '*.txt' \! -name 'directoryList.txt' | sed 's|-[0-9]\{3\}\.txt$||g'`
if [ "`basename $filemoves`" == "$line" ]
    then
    cp $filemoves $line/    
    echo "copied $filemoves to $line"
fi  
done
exec 0<&3

Things seem to work OK until I get to the if. I’m working across a number of *nix, so I have to be careful what arguments I’m throwing around (RHEL, FreeBSD, and possibly Mac OS X, too).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:10:18+00:00Added an answer on May 29, 2026 at 8:10 am

    Assuming your files really match the pattern above (everything before the last dash is the directory name) this should do it:

    for thefile in *.txt ; do mv -v $thefile ${thefile%-*}; done
    

    and if it tells you command line too long (expanding *.txt into 4900 files is a lot) try this:

    find . -name '*.txt' | while read thefile ; do mv -v $thefile ${thefile%-*} ; done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a directory with multiple sub directories that contain .doc files. Example: C:\Users\tmedina\Documents\testenviroment\Released\500\test0.doc
I have a directory Dir in which I have about 30 sub-directories. From these
I have the path /home/mine/new that contains files and sub directories. I want to
i have directory that has 2 sub-directories and that again has few sub-directory and
Here's what I'm trying to achieve. I have a directory with over 4200 sub-files/sub-directories,
I am going to have one directory with multiple sub-directories. I want to be
I'm on linux, and I have a directory with numerous sub-directories and items inside
I have a directory and a bunch of sub-directories like this: - directory1 (
I have a PHP script (index.php) that includes an index.html file in a sub-directory
Assume that you have a directory under subversion control, that contains some files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.