LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 02-10-2021, 10:52 AM   #1
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Rep: Reputation: Disabled
Question remove newline and tab characters from filename


Hello,

I've got some files that have \n and \t characters in the filenames that I'd like to remove.

This works great for the first newline:

rename $'\n' '' FILE*

I can run this again to remove the next new line but is it possible to capture multiple newlines with one command?

How to remove tabs? How to remove both at once?

# ls -lb
T1\tThisFileNameHas\tTabs
N1\nThisFileNameHas\nNewlines
TN1\tThis\nFileName\nHas\tTabsandNewlines
 
Old 02-10-2021, 11:13 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554

There'll be dozens of different ways to do this - one option is to use parameter expansion in the shell to replace the unwanted characters with a safer one.

For example, this creates a badly named file, fixes the filename, then renames it:
Code:
orig_filename=$'TN1\tThis\nFileName\nHas\tTabsandNewlines'
echo test > "$orig_filename"
new_filename="${orig_filename//[$'\t'$'\n']/-}"
mv --no-clobber "$orig_filename" "$new_filename"
Both newline and tab characters are replaced with the value between / and }" in the penultimate line - in the above example, a single hyphen - but you could remove that to have the characters removed instead. The double slashes // makes it change all occurrences instead of just the first.

The --no-clobber option to mv prevents any existing files being overwritten if there is a naming collision. (Allowing you to choose a different replacement string to avoid that collision).

 
  


Reply

Tags
rename, renaming



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tab separated file - remove CR/LF if it occurs before n tab characters ? thesnow Programming 5 06-12-2019 06:04 PM
match newline, space, newline vincix Programming 13 03-09-2017 03:14 PM
[SOLVED] sed command to replace 7th tab with newline tonyfreeman Programming 4 03-11-2011 04:36 PM
[SOLVED] How to replace newline pattern in file by other newline pattern in a shell script XXLRay Linux - Software 9 11-29-2010 07:57 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 09:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration