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 8606649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:08:54+00:00 2026-06-12T03:08:54+00:00

Problem In a directory there are files of the format: *-foo-bar.txt. Example directory: $

  • 0

Problem

In a directory there are files of the format: *-foo-bar.txt.

Example directory:

$ ls *-*
asdf-foo-bar.txt  ghjk-foo-bar.txt  l-foo-bar.txt     tyui-foo-bar.txt
bnm-foo-bar.txt   iop-foo-bar.txt   qwer-foo-bar.txt  zxcv-foo-bar.txt

Desired directory:

$ ls *.txt
asdf.txt  bnm.txt  ghjk.txt  iop.txt  l.txt  qwer.txt  tyui.txt  zxcv.txt

Solution 1

The first solution that came to my mind looks somewhat like this ugly hack:

ls *-* | cut -d- -f1 | sed 's/.*/mv "\0-foo-bar.txt" "\0.txt"/' > rename.sh && sh rename.sh

The above solution creates a script, on the fly, to rename the files. This solution also tries to parse the output of ls which is not a good thing to do as per http://mywiki.wooledge.org/ParsingLs.

Solution 2

This problem can be solved more elegantly with a shell script like this:

for i in *-*
do
    mv "$i" "`echo $i | cut -f1 -d-`.txt"
done

The above solution uses a loop to rename the files.

Question

Is there a way to solve this problem in a single line such that we do not have to explicitly script a loop, or generate a script, or invoke a new or the current shell (i.e. avoid sh, bash, ., etc. commands)?

  • 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-06-12T03:08:56+00:00Added an answer on June 12, 2026 at 3:08 am

    Have you tried the rename command?

    For example:

    rename 's/-foo-bar//' *-foo-bar.txt
    

    If you don’t have that available, I would use find, sed, and xargs:

    find . -maxdepth 1 -mindepth 1 -type f -name '*-foo-bar.txt' | sed 's/-foo-bar.txt//' | xargs -I{} mv {}-foo-bar.txt {}.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Example dump from the list of a directory: hello:3.1 GB world:1.2 MB foo:956.2 KB
Clients upload files in .doc format to a server directory, and the text within
There are bunch of files that are updated on a directory called: c:/files. These
The problem: I need to ensure that a particular directory hierarchy exists in the
My problem is that I want to redirect via JavaScript to a directory above.
We are experiencing a performance problem when communicating with the active directory using System.DirectoryServices.
I'm running into an odd problem with MEF's composition container and directory catalog where
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Updated Problem solved, I have some design problem here. The directory looks like that:
I have a little problem, I need to make directory from my c code

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.