I have to convert an entire directory using dos2unix
. I am not able to figure out how to do this.
find . -type f -print0 | xargs -0 dos2unix
Will recursively find all files inside current directory and call for these files dos2unix command
find
. In this example, it's the dot (i.e., the current directory). –
Hypesthesia \;; echo Hello
–
Cai find
invocation does not do this check (although that would be simple enough to add), but modern dos2unix
correctly skips binary files. –
Phototherapy find
with -print0
and xargs
, then you should probably add your own answer. -print0
is not Posix, so the change could have a negative effect on some users. Considering NickC already provided that variant of the answer, there's little reason to change CyberDem0n's answer. –
Evan .kitchen.yml
, or .gitignore
and those were not picked up. –
Gyve find . -type f -name '*.py' -print0 | xargs -0 dos2unix
–
Reactivate -i
switch to dos2unix
, or it will just print the converted files to stdout. –
Labarum .git
index, your git index will be corrupted. –
Gurgle .git
index corruption and preserve UTF-8 BOM, I've ended up doing find . -type f -print0 | xargs -0 dos2unix -ic0 | xargs -0 dos2unix -b
. This command touches only those files (thanks to -ic0
filtering option), that contain windows line breaks, all other files are skipped. -b
option is also helpful if your repository contains files with UTF-8 BOM, it preserves the BOM. –
Pinson git ls-files
instead of find
if you do anything with git repositories. –
Insignificancy dos2unix *
All the files in the current directory will be converted to unix format. –
Drawplate *
will not access hidden files or folders which begin with a dot (.
). –
Jalbert dos2unix
(or any other command) on your desired directory or path using multiple processes. –
Jalbert If it's a large directory you may want to consider running with multiple processors:
find . -type f -print0 | xargs -0 -n 1 -P 4 dos2unix
This will pass 1 file at a time, and use 4 processors.
n
up by an order of magnitude or two (depending on how many files we're talking about here) –
Hypoxia -n 50
. –
Jalbert A common use case appears to be to standardize line endings for all files committed to a Git repository:
git ls-files -z | xargs -0 dos2unix
Keep in mind that certain files (e.g. *.sln
, *.bat
) are only used on Windows operating systems and should keep the CRLF
ending:
git ls-files -z '*.sln' '*.bat' | xargs -0 unix2dos
If necessary, use .gitattributes
xargs
defaults to having all whitespace be a delimiter. The xargs
man page shows a --delimiter
option, but suggests using the --null
option when input might contain spaces. I have not tested this, but adding --null
to yours produces the following: git ls-files | xargs --null dos2unix
. –
Extern -z
for ls-files
). I've updated my answer. Thanks again! –
Aylesbury As I happened to be poorly satisfied by dos2unix, I rolled out my own simple utility. Apart of a few advantages in speed and predictability, the syntax is also a bit simpler :
endlines unix *
And if you want it to go down into subdirectories (skipping hidden dirs and non-text files) :
endlines unix -r .
endlines
is available here https://github.com/mdolidon/endlines
dos2unix
? Genuinely curious. –
Mustachio dos2unix
, with varying capabilities (some read UTF32 for example, while some don't ; endlines does not). There's only one endlines
, which capabilities are well known. 2/ liberal on input, not all dos2unix
are. 3/ efficient file tree exploration, designed to be fast and practical on tens of thousands of files. 4/ runs out of the box on OSX - which is less important now that Brew package exists. –
Anticipate It's probably best to skip hidden files and folders, such as .git.
So instead of using find
, if your bash
version is recent enough or if you're using zsh
, just do:
dos2unix **
Note that for Bash, this will require:
shopt -s globstar
....but this is a useful enough feature that you should honestly just put it in your .bashrc
anyway.
If you don't want to skip hidden files and folders, but you still don't want to mess with find
(and I wouldn't blame you), you can provide a second recursive-glob argument to match only hidden entries:
dos2unix ** **/.*
Note that in both cases, the glob will expand to include directories, so you will see the following warning (potentially many times over): Skipping <dir>, not a regular file.
dos2unix
alias that's affecting how arguments are expanded. What is the output of type dos2unix
on your system? –
Phototherapy dos2unix
and not the actual globstar
. It seems if I use dos2unix
like that it just blindly ignores files that are hidden (start with '.', eg '.vimrc')......? But the globstar
itself seems to work - the output of ls -a **
is as one would expect... –
Beriosova dos2unix
command never operates on the .vimrc
file. Not the behavior I expected - any insights appreciated :) –
Beriosova **
instead of find
is to "skip hidden files and folders, such as .git
". dos2unix
never sees the hidden files, because **
does not expand to show them. If you want to automatically run dos2unix
on hidden files and folders, you can use find
or dos2unix ** **/.*
The **/.*
will expand only the hidden files and folders, including .
(the root dir), ..
(the parent dir), and any other hidden entries in the current folder. –
Phototherapy type dos2unix
returns dos2unix is hashed (/usr/bin/dos2unix)
–
Beriosova dos2unix
simply prints Skipping <dir>, not a regular file.
when run on a directory, so running on ..
and .
is safe.) Additionally, combining ls
with a glob is not a good way to check how the glob is expanded; use echo
instead: echo **
will print the arguments that dos2unix
receives from dos2unix **
. –
Phototherapy **
implied, and using echo
makes it clear. –
Beriosova **
is to make globs themselves exercise recursive descent. –
Phototherapy For any Solaris users (am using 5.10, may apply to newer versions too, as well as other unix systems):
dos2unix doesn't default to overwriting the file, it will just print the updated version to stdout, so you will have to specify the source and target, i.e. the same name twice:
find . -type f -exec dos2unix {} {} \;
I've googled this like a million times, so my solution is to just put this bash function in your environment.
.bashrc
or .profile
or whatever
dos2unixd() {
find $1 -type f -print0 | xargs -0 dos2unix
}
Usage
$ dos2unixd ./somepath
This way you still have the original command dos2unix
and it's easy to remember this one dos2unixd
.
I think the simplest way is:
dos2unix $(find . -type f)
I have had the same problem and thanks to the posts here I have solved it. I knew that I have around a hundred files and I needed to run it for *.js files only.
find . -type f -name '*.js' -print0 | xargs -0 dos2unix
Thank you all for your help.
for FILE in /var/www/html/files/*
do
/usr/bin/dos2unix FILE
done
If there is no sub-directory, you can also take
ls | xargs -I {} dos2unix "{}"
dos2unix *
is simpler and will actually be more robust than this. (It's generally not recommended to pipe the output of ls
, because it's a formatting tool and *
is more reliable for programmatic usage.) –
Phototherapy ls
is not good –
Ferdy © 2022 - 2024 — McMap. All rights reserved.