Rename directory removing last characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename directory removing last characters
# 1  
Old 11-28-2017
Rename directory removing last characters

Hello -

I was looking to write a simple script trying to rename sub-directories chopping off the last n characters.

For example:

In /home/myname/dir there are three sub-directories: directory1_1, directory2_2, and directory3_3.

Is there a simple script to chop off the last couple characters so you are left
with directory1, directory2, and directory3 under /home/myname/dir ?

Thank you

Last edited by rbatte1; 11-29-2017 at 07:30 AM.. Reason: Added ICODE tags for clarity
# 2  
Old 11-28-2017
Hi.

If you want to "chop off" the last _* of a string, you can use something like:
Code:
${dirname%_*}

where dirname is the directory name.

e.g.
Code:
dirname=/some/path/directory1_1
echo ${dirname%_*}
/some/path/directory1

The question is, how do you get a list of dirnames?

Do you have an example of how you find these directories (a script, or something) such that you can "chop off" that ending?

Last edited by Scott; 11-29-2017 at 03:47 AM.. Reason: fixed "cut and paste" error in output
# 3  
Old 11-29-2017
Hi Scott -

Thank you for responding. Can you use a for loop (like for files in a directory) but use it for subdirectories?

---------- Post updated at 04:15 PM ---------- Previous update was at 03:47 PM ----------

Hi Scott -

I used the following and it works but getting error when trying to remove the echo:

Code:
 cd /some/path
  
 for d in */; do

 echo ${d%_*}
  
 done

It says 'directory1: command not found'

Thanks!

Last edited by Scott; 11-29-2017 at 05:25 PM.. Reason: Replaced ICODE tags with CODE tags
# 4  
Old 11-29-2017
Ah yes, sorry, I pasted the wrong thing.

Should be:
Code:
echo mv $d ${d%_*}

(then remove the echo if the output looks OK)
This User Gave Thanks to Scott For This Post:
# 5  
Old 11-30-2017
Perfect!

Thanks Scott!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename File Name with Special Characters

I am trying to rename files with spaces and other characters and not able to be successful. FileNames: UPLOAD REFERENCE.xls UPLOAD MASS REFERENCE.XLS find /UPLOAD REFERENCE/ -depth -type f -name "* *" -exec rename " " "_" "{}" ";" The above one is successful to replace spaces... (1 Reply)
Discussion started by: eskay
1 Replies

2. Shell Programming and Scripting

Recursivly rename folders removing characters as required

HI guys here's hoping some on pout the can help I have a large library of epub and mobi file creates some what by calibre. Output of tree listing below I would like to recursively rename the directories removing the brackets and numbers I have been scratching my head over... (4 Replies)
Discussion started by: dunryc
4 Replies

3. Shell Programming and Scripting

How to rename all files at a time by appending some characters at the begining?

Hi I have a list a filename in a directory starting with particular pattern for example: abc_1234.txt abc_7565.txt abc_7676.txt abc_7765.txt i need to rename all these files by appending bck. or bck_ Expected output: bck.abc_1234.txt bck.abc_7565.txt bck.abc_7676.txt... (1 Reply)
Discussion started by: Little
1 Replies

4. UNIX for Dummies Questions & Answers

Removing directory with leading hyphen from root directory

I know that this basic question has been asked many times and solutions all over the internet, but none of the are working for me. I have a directory in the root directory, named "-p". # ls -l / total 198 <snip> drwxr-xr-x 4 root root 4096 Dec 3 14:18 opt drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: edstevens
2 Replies

5. Shell Programming and Scripting

Filename rename with characters of file

Hi, I need a bit of help. I've used awk to get the first 7 characters of a file - awk '{print substr($0,0,7)}' test.csv How do I now take this variable to rename test.csv to variable.csv ? Any help or advice would be greatly appreciated! (2 Replies)
Discussion started by: sianm
2 Replies

6. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

7. UNIX for Dummies Questions & Answers

removing first 7 characters of directory names

Hi, I'm relatively new to unix, and would like to change the following files in a particular directory. The files have names like: M10_90_Phcn402_3F.ab1 M10_94_Sput402_3F.ab1 M11_92_Abrg402_3R.ab1 M10_91_Cdel402_3F.ab1 M11_90_Phcn402_3R.ab1 M12_84_Sput402_3R.ab1... (4 Replies)
Discussion started by: euspilapteryx
4 Replies

8. HP-UX

Removing ^D and ^H characters

Hi, I have a very huge file and it contains some unprintable characters like ^H and ^D. If I try to remove using cat test1.ser| tr -d '\136 110'>newfile1 it is only removing ^and all spaces in the file. How can I remove these characters (^D ^H) and keep my spaces as it is? Thanks &... (1 Reply)
Discussion started by: arsheshadri
1 Replies

9. Shell Programming and Scripting

Help with removing characters like ^M

I have few files in unix which are in dos format. While I am copying these files, ^M, ^@, etc characters are being generated. I tried dos2unix command in Linux and it doesn't work. I tried sed to remove these characters but they won't go. I came to about this 'tr' command and tried to use it... (16 Replies)
Discussion started by: chiru_h
16 Replies
Login or Register to Ask a Question