Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > General
User Name
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!


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

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


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
Old 02-10-2021, 12:13 PM   #2
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,066

Rep: Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871Reputation: 871

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:
echo test > "$orig_filename"
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).



rename, renaming

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 07:04 PM
match newline, space, newline vincix Programming 13 03-09-2017 04:14 PM
[SOLVED] sed command to replace 7th tab with newline tonyfreeman Programming 4 03-11-2011 05: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 08:57 AM
VI replacement: newline for newline not working! jhwilliams Linux - Software 3 08-16-2007 07:11 PM > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 04:06 AM.

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