Linux convert windows file to linux

This article seeks to achieve one objective; the conversion of a Windows File to a UNIX file without changing the format of the resulting file.

You may ask yourself, what is the purpose of this article? Why convert a Windows file to adapt to a UNIX environment like Linux? Isn’t Linux all-powerful? The exceptional capabilities of the Linux operating system do not spare it from incompatible displays of files transferred from other computing platforms.

Just because you can open a file on a Linux environment does not imply that you have full control over how the file’s texts should be displayed.

[ You might also like: How to Find Files Containing Specific Text String in Linux ]

You will encounter instances where a file’s texts or words are jammed together on a single giant line. In other instances, the same file texts’ displays might not have line breaks or sentence spacing.

A common attribute of raw Windows files opened in UNIX systems like Linux is the unavoidable end-of-line display of ^M or Ctrl-M characters.

This article guide seeks to achieve one objective; the conversion of a Windows File to a UNIX file without changing the format of the resulting file.

Ways to Convert Windows File to Unix Format in Linux

We can achieve the objective of our article through several methods. These methods allow us to convert a Windows file to a UNIX file and still retain the original format of the Windows file.

Convert Windows File to Unix Using dos2unix Command

Depending on your Linux operating system distribution, you can install the dos2unix command-line tool from one of the following commands:

$ sudo apt-get install dos2unix     [On Debian, Ubuntu and Mint]
$ sudo yum install dos2unix         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/dos2unix  [On Gentoo Linux]
$ sudo pacman -S dos2unix           [On Arch Linux]
$ sudo zypper install dos2unix      [On OpenSUSE]    

The command syntax for using the dos2unix tool is as follows:

$ dos2unix Your_Windows_File Final_Unix_File

So if you have a sample file created on a Windows computing system and want to open it on a Linux computing system without compromising its format, you would use the following command.

$ dos2unix windows_readme.txt unix_readme.txt

Before we run the above command, we need to create a blank unix_readme.txt file that will accommodate the converted file.

$ touch unix_readme.txt
$ dos2unix windows_readme.txt unix_readme.txt    

Convert Windows File to Unix Using dos2unix

Convert Windows File to Unix Using dos2unix

As per the screen capture, your converted Windows file should comfortably adapt to any Unix environment.

Using AWK Command – Convert Windows File to Unix

The awk command is pre-installed on all modern full-fledged UNIX computing systems like Linux. To convert our sample Windows file, we would implement the awk command in the following manner:

$ awk '{ sub("r$", ""); print }' windows_readme.txt > new_unix_readme.txt

Awk Convert Windows File to Unix

Awk Convert Windows File to Unix

As you have noted, with the awk command, we don’t need a pre-existing blank Linux file to accommodate the converted Windows file. The command creates and populates the Unix file version of the Windows file on its own.

Using tr Command – Convert Windows File to Unix

If most of the Windows files you open on your Linux environment have unnecessary Ctrl-Z and carriage return characters, then you will appreciate what the tr command has to offer.

Supposing our sample Windows file is a victim of such characters, removing them will require implementing the following command:

$ tr -d '' < windows_readme.txt > polished_unix_readme.txt

tr Convert Windows File to Unix

tr Convert Windows File to Unix

The inbuilt nature of the tr command also generates the resulting UNIX file without the need for its pre-creation.

The flexibility of the three discussed approaches to converting any editable Windows file to UNIX file format should save you from the headaches of having to manually edit your downloaded or transferred Windows files to remove unwanted characters and spaces while on a Linux environment.

I am having trouble with Windows files running as part of a shell script on a Linux box and I was wondering if it is possible to «convert» a Windows created file to a Linux one using Java or possibly a Linux command?

For example:

If I upload a CSV file created in Windows, then on the server using nano I can see the file was saved in DOS Format. I can toggle between DOS and Linux format using M-D and save it as a Linux file.

My question is whether it is possible to achieve this via Java (<-preference) or through a Linux command?

I have tried stripping carriage returns sed -i 's/{ctrl-v}{ctrl-m}//g' [file] but this does not help with the issue.

asked Apr 18, 2012 at 11:29

My Head Hurts's user avatar

My Head HurtsMy Head Hurts

37.2k16 gold badges74 silver badges117 bronze badges

2

dos2unix and unix2dos unix commands, from the dos2unix package.

answered Apr 18, 2012 at 11:31

Šimon Tóth's user avatar

Šimon TóthŠimon Tóth

35k19 gold badges104 silver badges148 bronze badges

2

You can use BufferedReader with a contained FileReader to get the File line by line and then do whatever you want with that lines (e.G. push them into another File that has the correct line endings.

File relFile = new File(".....");
BufferedReader read = new BufferedReader(new FileReader(relFile));
File targetFile = new File("....");
FileWriter fwri = new FileWriter(targetFile);
String line;
while ((line = read.readLine()) != null) {
    fwri.write(line+"n"); // if you want the system line delimiter use the System property for that.
}
fwri.flush();
fwri.close();

answered Apr 18, 2012 at 11:33

Angelo Fuchs's user avatar

0

if you don’t have dos2unix utility available but you have vim or vi installed

open your file:

vi yourfile.csv or vim yourfile.csv

To change the file format to unix type:

:setlocal ff=unix

Then save the file:

:wq!

Reference: http://vim.wikia.com/wiki/File_Format

answered Aug 2, 2017 at 12:57

brunocrt's user avatar

brunocrtbrunocrt

6709 silver badges11 bronze badges

use vim to check your encoding format:

:set fileencoding

in windows, it is normally gb2312, but in linux it is utf-8.

So you’d better to convert it to linux format by following method:

:set fileencoding=utf-8

and then save it by :wq!

answered Aug 5, 2014 at 18:07

Haimei's user avatar

HaimeiHaimei

12.3k3 gold badges49 silver badges35 bronze badges

Содержание

  1. How to Convert a Windows File to a UNIX File
  2. Ways to Convert Windows File to Unix Format in Linux
  3. Convert Windows File to Unix Using dos2unix Command
  4. Using AWK Command – Convert Windows File to Unix
  5. Using tr Command – Convert Windows File to Unix
  6. DOS to Unix: Commands and Examples
  7. Converting Files on Linux
  8. Option 1: Converting DOS to UNIX with dos2unix Command
  9. Option 2: Converting UNIX to DOS using the unix2dos Command
  10. Option 3: Using the sed Command
  11. Option 4: Using the tr Command
  12. Option 5: Using the Vim Text Editor
  13. Option 6: Using a Perl One Liner
  14. Example: Converting a Sample File from DOS to Unix Format

How to Convert a Windows File to a UNIX File

You may ask yourself, what is the purpose of this article? Why convert a Windows file to adapt to a UNIX environment like Linux? Isn’t Linux all-powerful? The exceptional capabilities of the Linux operating system do not spare it from incompatible displays of files transferred from other computing platforms.

Just because you can open a file on a Linux environment does not imply that you have full control over how the file’s texts should be displayed.

You will encounter instances where a file’s texts or words are jammed together on a single giant line. In other instances, the same file texts’ displays might not have line breaks or sentence spacing.

A common attribute of raw Windows files opened in UNIX systems like Linux is the unavoidable end-of-line display of ^M or Ctrl-M characters.

This article guide seeks to achieve one objective; the conversion of a Windows File to a UNIX file without changing the format of the resulting file.

Ways to Convert Windows File to Unix Format in Linux

We can achieve the objective of our article through several methods. These methods allow us to convert a Windows file to a UNIX file and still retain the original format of the Windows file.

Convert Windows File to Unix Using dos2unix Command

Depending on your Linux operating system distribution, you can install the dos2unix command-line tool from one of the following commands:

The command syntax for using the dos2unix tool is as follows:

So if you have a sample file created on a Windows computing system and want to open it on a Linux computing system without compromising its format, you would use the following command.

Before we run the above command, we need to create a blank unix_readme.txt file that will accommodate the converted file.

Convert Windows File to Unix Using dos2unix

As per the screen capture, your converted Windows file should comfortably adapt to any Unix environment.

Using AWK Command – Convert Windows File to Unix

The awk command is pre-installed on all modern full-fledged UNIX computing systems like Linux. To convert our sample Windows file, we would implement the awk command in the following manner:

Awk Convert Windows File to Unix

As you have noted, with the awk command, we don’t need a pre-existing blank Linux file to accommodate the converted Windows file. The command creates and populates the Unix file version of the Windows file on its own.

Using tr Command – Convert Windows File to Unix

If most of the Windows files you open on your Linux environment have unnecessary Ctrl-Z and carriage return characters, then you will appreciate what the tr command has to offer.

Supposing our sample Windows file is a victim of such characters, removing them will require implementing the following command:

tr Convert Windows File to Unix

The inbuilt nature of the tr command also generates the resulting UNIX file without the need for its pre-creation.

The flexibility of the three discussed approaches to converting any editable Windows file to UNIX file format should save you from the headaches of having to manually edit your downloaded or transferred Windows files to remove unwanted characters and spaces while on a Linux environment.

Источник

DOS to Unix: Commands and Examples

Home » SysAdmin » DOS to Unix: Commands and Examples

Files created in DOS/Windows use carriage return (r) and line feed (n) for line endings. However, files in Unix/Linux solely use line feed.

Therefore, when transferring a file from one system to another, make sure to convert the files.

In this tutorial, you will learn how to transfer files from DOS to Unix and vice-versa.

Converting Files on Linux

There are several ways you can transfer files to use the appropriate line endings. Convert the files using the:

  • dos2unix command
  • unix2dos command
  • sed command (see Linux sed command tutorial)
  • tr command
  • Vi/Vim text editor (see Vim commands cheat sheet)
  • perl one-liner command

Option 1: Converting DOS to UNIX with dos2unix Command

The simplest way to convert line breaks in a text file is to use the dos2unix tool.

Install the tool by running the command:

If you download a file created in DOS/Windows onto your Linux system, you can convert it using the dos2unix command:

The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name. Doing so creates a backup file under the same name and the .bak extension.

Option 2: Converting UNIX to DOS using the unix2dos Command

To convert a file created on a Linux system into the DOS format, run the command:

Alternatively, add the -b attribute to save the original file as backup:

Option 3: Using the sed Command

You can also use the sed (stream editor) command to remove the carriage return line endings. The command syntax to do so is:

Instead of just typing ^M , press Ctrl+V followed by Ctrl+M to enter the carriage return symbol. When using the sed command, specify the name of the DOS file [original_file_name] and how you want to name the converted file [converted_file_name] .

To change the file format from Unix to DOS, use the command:

Note: Learn more about sed by referring to our article How to Use Sed to Find and Replace a String in a File.

Option 4: Using the tr Command

Another way to convert a file into the Unix format is to remove r line endings with the tr command. The tr command is a command line utility for translating or deleting characters.

Use the command in the following format:

Option 5: Using the Vim Text Editor

You can also remove carriage return line endings from files in DOS format using the Vi/Vim text editor.
Open the file in Vi/Vim:

Then press : and type in the following Vi/Vim command (making sure you type Ctrl+V then Ctrl+m instead of ^m ):

Option 6: Using a Perl One Liner

Lastly, you can use a Perl one-liner command to delete all r line endings. Pearl on-liners are scripts that fit in a single line of code.

To replace all carriage return and line feed endings with just line feeds:

1. Open the file in the Vi/Vim text editor.

2. Press : to prompt the command line.

3. Type in the following command and press Enter:

You should see the the changes in the file right away.

Note: Need help finding files on your Linux system? Take a look at how to use the find command or how to show hidden files in Linux.

Example: Converting a Sample File from DOS to Unix Format

Let’s say you downloaded a file named sample-dos-file.

By opening the file using the Vim/Vi text editor, you would see that after each line ending there is ^M (a carriage return).

Another way to see the file uses both carriage return and line feed for line endings is to view the file in octal values.

To do so, run the command:

The output displays the content of the file with its octal values, as in the image below. You can see that each line end is marked with the octal values 015 (r) and 012 (n).

Now, to convert the file and remove all carriage return endings, you would run the command:

Open the same file in Vim/Vi. It doesn’t include any ^M symbols signaling the line ending.

To confirm the file is in Unix format, you can also open view the content in octal values. The output should display just the 012 values for n.

This article showed six ways to convert a DOS file into a Unix format and vice versa.

The simplest and recommended way is using the dos2unix command. If you cannot utilize dos2unix , you have five other options to choose from.

Источник

Life would be a lot easier if we could live in a Linux-only world and if applications never required data from other sources. However, the need to get data from Windows, MS-DOS, or old Macintosh systems is all too common. This kind of import process requires some conversions to solve file format differences; otherwise, it would be impossible to share data, or file contents would be imported incorrectly. The easiest way to transfer data between systems is by using plain text files or common formats like comma-separated value (CSV) files. However, converting such files from Windows or Mac OS results in formatting differences for the newline characters and character encoding. This article explains why we have these problems and shows ways to solve them.

The newline problem

Every operating system uses a special character (or sequence of characters) to signify the end of a line of text. They cannot use standard, common characters to represent the line end, because those could appear in normal text, so they use special, nonprinting characters — but each operating system uses different ones:

  • Linux and Mac OS X inherited the Unix style of using LF (line feed, an ASCII control character) at the end of each line.
  • Older Macintosh systems use CR (carriage return, another ASCII control character).
  • Windows uses a pair of characters — both a CR and an LF.

To check what a particular text file uses to indicate a new line, try hexdump, which lets you inspect the contents of a file at the byte level. I prepared two three-line files — one on a Linux system and one on a Windows machine — and dumped their contents. The file gets dumped 16 characters at a time, showing both the actual characters and their octal equivalents. Notice that the Linux file has a n character at the end of each line, while the Windows version uses r and n. An older Macintosh file would have used a single r character instead.

> cat test.linux This is the first line of a Linux file. This is the second line. Here's the last line. > hexdump -cb test.linux 0000000 T h i s i s t h e f i r s 0000000 124 150 151 163 040 151 163 040 164 150 145 040 146 151 162 163 0000010 t l i n e o f a L i n u 0000010 164 040 154 151 156 145 040 157 146 040 141 040 114 151 156 165 0000020 x f i l e . n T h i s i s 0000020 170 040 146 151 154 145 056 012 124 150 151 163 040 151 163 040 0000030 t h e s e c o n d l i n e . 0000030 164 150 145 040 163 145 143 157 156 144 040 154 151 156 145 056 0000040 n H e r e ' s t h e l a s t 0000040 012 110 145 162 145 047 163 040 164 150 145 040 154 141 163 164 0000050 l i n e . n 0000050 040 154 151 156 145 056 012 0000057 > cat test.windows This is the first line on a Windows file. This is the second line. Here's the last line. > hexdump -cb test.windows 0000000 T h i s i s t h e f i r s 0000000 124 150 151 163 040 151 163 040 164 150 145 040 146 151 162 163 0000010 t l i n e o n a W i n d 0000010 164 040 154 151 156 145 040 157 156 040 141 040 127 151 156 144 0000020 o w s f i l e . r n T h i s 0000020 157 167 163 040 146 151 154 145 056 015 012 124 150 151 163 040 0000030 i s t h e s e c o n d l i 0000030 151 163 040 164 150 145 040 163 145 143 157 156 144 040 154 151 0000040 n e . r n H e r e ' s t h e 0000040 156 145 056 015 012 110 145 162 145 047 163 040 164 150 145 040 0000050 l a s t l i n e . r n 0000050 154 141 163 164 040 154 151 156 145 056 015 012 000005c

To convert files from Windows to Linux, you can use the appropriately titled dos2unix command. The simplest way to convert test.windows to the Linux format would be with dos2unix test.windows, but you can also use the command in stream fashion — for example, dos2unix <test.windows >test.windows.fixed. Check all possible options with dos2unix -h or man dos2unix.

An old-fashioned Macintosh text file requires changing CR characters to LF ones, so you could use the tr (translate) command with tr "15" "12" <anOldMacintoshFile >theNewLinuxFile, which simply changes each CR (octal 015) into LF (octal 012). With tr, you could also use the -d (delete) option to remove CR characters from a Windows file, thus giving you a valid Linux file. The last conversion shown in the previous paragraph could also be done with tr -d "15" <test.windows >test.windows.fixed, and the results would be identical.

The encoding problem

English and other languages include some special typographical characters in addition to the normal 26 letters. Have you ever watched the movie Æon Flux or sent in a curriculum vitæ? If you have to import German text, you’ll find lots of vowels with an umlaut on them, and some ß characters as well. Spanish adds ň and acute accents to the mix, and French has grave and circumflex accents (“pie à la mode,” anybody?). (If you need to type these characters on a standard English keyboard, check out our article on how to customize your keyboard.)

You don’t need Unicode for Standard English: you can do perfectly well with ASCII characters, which includes plain unaccented letters from A to Z, digits from 0 to 9, and some punctuation characters. If you deal with only the Latin alphabets used in Western European languages, you might also get files encoded in ISO 8859-1 (also informally known as Latin-1), which lets you use just a single byte per character at the cost of not being able to represent more foreign languages. However, some languages deal with a wider character set and require Unicode, which supports more than 100,000 characters in dozens of languages. Unicode (also known as the ISO 10646 standard) extends ASCII, but where ASCII requires one byte for each character, Unicode requires more. To achieve compatibility between Unicode and ASCII, the UTF-8 character encoding is generally used. UTF-8 uses just a single byte for ASCII characters (so any ASCII file is by default a valid UTF-8 file) and only goes up to more bytes per character for foreign letters and other symbols.

While UTF-8 and Latin-1 do use the same representation for ASCII characters, they differ for other characters; therefore, in order to process the file correctly, you must know its format, or all non-ASCII characters might end up garbled.

Check out the following files, with the same Spanish text (“¡Que la Fuerza te acompañe!”, or “May the Force be with you!”) in both formats:

> hexdump -cb test.force.utf8 0000000 302 241 Q u e l a F u e r z a 0000000 302 241 121 165 145 040 154 141 040 106 165 145 162 172 141 040 0000010 t e a c o m p a 303 261 e ! n 0000010 164 145 040 141 143 157 155 160 141 303 261 145 041 012 000001e > hexdump -cb test.force.latin1 0000000 241 Q u e l a F u e r z a t 0000000 241 121 165 145 040 154 141 040 106 165 145 162 172 141 040 164 0000010 e a c o m p a 361 e ! n 0000010 145 040 141 143 157 155 160 141 361 145 041 012 000001c

If you check the foreign characters (the inverted exclamation sign at the beginning and the ñ near the end), you can verify that UTF-8 uses two bytes for each, while Latin-1 requires just one. Also, note that “normal” ASCII characters are the same in both formats.

Determining what format and encoding you require depends on your particular application. While most Linux programs work with UTF-8, many others use Latin-1, and some are even able to use both. You first need to learn what your application expects, and then convert the text file to that format, if necessary. Fortunately, you can easily accomplish the required translation in both directions (either from or to UTF-8) by using the recode command.

recode offers many options (use recode --help or info recode for a more thorough description), and you can use the program to convert to and from many different formats (try recode -l to get the list of supported formats). However, for simple conversions, it’s enough to do recode UTF-8..ISO-8859-1 test.force.utf8 to get a Latin-1 version, or recode ISO-8859-1..UTF-8 test.force.latin1. Depending on the specific conversion you need, the newline problem might be taken care of automatically, but check the documentation (or give it a try and see what happens) for each specific case.

In conclusion

Processing text files from other operating systems is not a straightforward process, but Linux provides tools to make the job easy. No matter what format a file is in, you can automate the required conversion steps and deal with the inconvenience of incompatible formats.

A Quick Intro to End-Of-Line

Most people don’t realise that when they hit the Enter key to create a new paragraph in a text file, something very different is going on behind the scenes in the three major operating systems: Windows, Macintosh and Linux. The “end-of-line delimiter” (often expressed as “End-Of-Line“, “End of Line“, or just “EOL“) – which some of you know as the “line break” or “newline” – is a special character used to designate the end of a line within a text file.

UNIX-based operating systems (like all Linux distros and BSD derivatives) use the line feed character (n or <LF>), “classic” Mac OS uses a carriage return (r or <CR>), while DOS/Windows uses a carriage return followed by a line feed (rn or <CR><LF>). Now that Mac OS X is based on FreeBSD‘s file system, it follows the UNIX convention.

Now, the reason most people don’t know about all this is because nobody really should have to. But while users of Linux distros and Mac OS can open Windows text files in basically any available editor and not even know the difference, the same can’t be said for Windows users opening files created in one of the other operating systems.

If you type up a simple text file in Ubuntu and save it in the default “Unix/Linux” format, in Windows it will appear as one continuous paragraph, with black squares where the line breaks (or new paragraphs) should be. While you can open the file in a more advanced text editor (or proper word processor) to view it as it should look, others you’ve sent it to are just likely to double-click it and let it open in Notepad (which can only handle MS-DOS EOL).

Occasionally, the reverse is the issue, but you can convert Windows text files to UNIX easily with Gedit, as well as convert them via the terminal, so hopefully the following guide will be of use.

For more detailed info on End-Of-Line, go to the Wikipedia page.

Or if you’re wanting to do the reverse, check out how to convert to Windows format via the terminal and with Save As… in Gedit.

Converting Windows EOL to Linux via the Terminal

If you find the text editor you’re using to display Windows files in Ubuntu shows ^M instead of a line break (not very likely with even the most lightweight text editors, but something you’ll probably come across if you display the text in a terminal), don’t worry – just convert them to Unix/Linux format.

While you can actually open them in Gedit and use Save As… to save over them (or to create copies) in the correct format, for more than a couple of files this would be the long, complicated solution.

By far the quickest and easiest approach is to convert the offending files via the command-line. This way, you could batch-convert hundreds of such files at once, not have to do them individually.
There are actually quite a few ways to do this, but we’ll look at a couple of tiny packages you can install, and the related commands to use.

The first – the tofrodos package – is undoubtedly the most widely-used, so we’ll look at that in detail – especially since many of the guides out there are outdated, since the commands it contains have been renamed.

The second is a little package called flip, and since it’s tiny and won’t cause any issues, it’s worth installing as a backup (just in case. I found it useful after trying to get tofrodos going on a new system, before I found out the commands were changed).

There is no actual command tofrodos, as it is just the package that contains the commands todos and fromdos. Currently, the vast majority of online guides will list the commands as unix2dos and dos2unix, but as the developer states:

With this release the symlinks “unix2dos” and “dos2unix” are dropped from the package. This will allow the introduction of the original dos2unix package, which also supports conversion to MacOS style files.

So now you can choose to use either todos (to convert to Windows) and fromdos (to convert to Linux), or just fromdos with options (fromdos -u to convert to DOS, and fromdos -d to convert to UNIX, though obviously the -d option really isn’t needed, as it is the default behaviour for the fromdos command).

We’ll use fromdos, as it is easier to remember, and show how to alter a single file, or all text files in a given folder. When you’re ready to proceed, open a terminal in the folder containing the text file(s) and use one of the following commands (note that for the purpose of illustration, the .txt suffix is used, but you can specify any other extension for your text files).

To Convert to UNIX/Linux via Terminal:

Single file (remember to replace filename.txt with the actual name of the file)

fromdos filename.txt

All text files in a folder (if the extension differs to .txt, simply replace it in the command)

fromdos *.txt

Similarly, flip is easy to use:

flip -u filename.txt (or flip -u *.txt for multiple files)

Converting Windows EOL to Linux with Gedit

It’s actually very easy to convert text files with Windows EOL to Unix/Linux in Ubuntu using the default Text Editor, Gedit. Simply open the files, choose Save As…, go to Line Ending in the dialogue box and choose Unix/Linux instead of Windows. While that is easy enough, for more than one or two you’d really want to save yourself some time and hassle and perform a batch-conversion via the terminal.

☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻☻

Did this information make your day? Did it rescue you from hours of headache? Then please consider making a donation via PayPal, to buy me a donut, beer, or some fish’n’chips for my time and effort! Many thanks!

Buy Ubuntu Genius a Beer to say Thanks!

How do I run a .TXT file in Linux?

Here is an example:

  1. $cd /tmp.
  2. $mkdir run.
  3. $cd run.
  4. $nano text. txt.
  5. #!/usr/bin/env perl.
  6. print “Hello Worldn”;
  7. ^x to save file.
  8. $chmod +x text. txt.

To convert a Windows file to a UNIX file, enter the following command:

  1. awk ‘{ sub(“r$”, “”); print }’ windows.txt > unix.txt.
  2. awk ‘sub(“$”, “r”)’ uniz.txt > windows.txt.
  3. tr -d ‘1532’ < winfile.txt > unixfile.txt.

1 апр. 2014 г.

How do I transfer files from Windows to Linux?

To transfer data between Windows and Linux, simply open FileZilla on a Windows machine and follow the below steps:

  1. Navigate and open File > Site Manager.
  2. Click a New Site.
  3. Set the Protocol to SFTP (SSH File Transfer Protocol).
  4. Set the Hostname to the IP address of the Linux machine.
  5. Set the Logon Type as Normal.

12 янв. 2021 г.

How do you change a TXT file in Linux?

How to edit files in Linux

  1. Press the ESC key for normal mode.
  2. Press i Key for insert mode.
  3. Press :q! keys to exit from the editor without saving a file.
  4. Press :wq! Keys to save the updated file and exit from the editor.
  5. Press :w test. txt to save the file as test. txt.

How do I run a text file in command prompt?

On a Windows machine, we can open a text file from command prompt by just giving the file name. For example to open a text file named file1. txt, we just need to type file1. txt in the command prompt and press ‘Enter’.

How do I run a script in a text file?

This is called a script. Right click on the text file, select properties, select permission, mark the “Let this file be executed” text box. Now you can execute it just by double clicking on the file.

How do I open a Unix file in Windows?

Logging into an UNIX server

  1. Download PuTTY from here.
  2. Install using the default settings on your computer.
  3. Double-click the PuTTY icon.
  4. Enter the UNIX/Linux server hostname in the ‘Host Name’ box, and press the ‘Open’ button at the bottom of the dialog box.
  5. Enter your username and password when prompted.

How do I open a Linux file in Windows?

Ext2Fsd. Ext2Fsd is a Windows file system driver for the Ext2, Ext3, and Ext4 file systems. It allows Windows to read Linux file systems natively, providing access to the file system via a drive letter that any program can access. You can have Ext2Fsd launch at every boot or only open it when you need it.

How do you change a file format in Unix?

HowTo: Unix / Linux Rename File Extension From . OLD to . NEW

  1. mv old-file-name new-file-name. To rename a file called resume.docz to resume.doc, run:
  2. mv resume.docz resume.doc ls -l resume.doc. To rename file extension from .txt to .doc, enter:
  3. mv foo.txt foo.doc ls -l foo.doc ## error ## ls -l foo.txt. To fix the extension of all your .txt files, enter::
  4. rename .txt .doc *.txt.

12 мар. 2013 г.

How to share files between a Linux and Windows computer

  1. Open the Control Panel.
  2. Go to Network and Sharing Options.
  3. Go to Change Advanced Sharing Settings.
  4. Select Turn on Network Discovery and Turn on File and Print Sharing.

31 дек. 2020 г.

How do I download files from Linux to Windows?

  1. Step 1: Download pscp. https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html. …
  2. Step 2: Get familiar with the pscp commands. …
  3. Step 3: Transfer file from your Linux machine to Windows machine. …
  4. Step 4: Transfer file from your Windows machine to Linux machine.

How do I copy and paste from Linux to Windows?

Ctrl+Shift+C and Ctrl+Shift+V

If you highlight text in the terminal window with your mouse and hit Ctrl+Shift+C you’ll copy that text into a clipboard buffer. You can use Ctrl+Shift+V to paste the copied text into the same terminal window, or into another terminal window.

How do I open and edit a file in Linux?

Edit the file with vim:

  1. Open the file in vim with the command “vim”. …
  2. Type “/” and then the name of the value you would like to edit and press Enter to search for the value in the file. …
  3. Type “i” to enter insert mode.
  4. Modify the value that you would like to change using the arrow keys on your keyboard.

21 мар. 2019 г.

How do you overwrite a file in Linux?

To overwrite one file’s content to another file. use cat eg. If you have output that can have errors, you may want to use an ampersand and a greater than, as follows: my_task &> ‘Users/Name/Desktop/task_output.

How do I edit a file without opening it in Linux?

Yes, you can use ‘sed’ (the Stream EDitor) to search for any number of patterns or lines by number and replace, delete, or add to them, then write the output to a new file, after which the new file can replace the original file by renaming it to the old name.

Introduction

Files created in DOS/Windows use carriage return (r) and line feed (n) for line endings. However, files in Unix/Linux solely use line feed.

Therefore, when transferring a file from one system to another, make sure to convert the files.

In this tutorial, you will learn how to transfer files from DOS to Unix and vice-versa.

Guide on how to transfer DOS to UNIX files.

Converting Files on Linux

There are several ways you can transfer files to use the appropriate line endings. Convert the files using the:

  • dos2unixcommand
  • unix2dos command
  • sed command (see Linux sed command tutorial)
  • tr command
  • Vi/Vim text editor (see Vim commands cheat sheet)
  • perl one-liner command

Option 1: Converting DOS to UNIX with dos2unix Command

The simplest way to convert line breaks in a text file is to use the dos2unix tool.

Install the tool by running the command:

sudo apt install dos2unix

or:

sudo dnf install dos2unix
The command to install dos2unix tool on Linux systems.

If you download a file created in DOS/Windows onto your Linux system, you can convert it using the dos2unix command:

dos2unix [file_name]

The command converts the file without saving it in the original format. If you want to save the original file, add the -b attribute before the file name. Doing so creates a backup file under the same name and the .bak extension.

dos2unix -b [file_name]

Option 2: Converting UNIX to DOS using the unix2dos Command

To convert a file created on a Linux system into the DOS format, run the command:

unix2dos [file_name]

Alternatively, add the -b attribute to save the original file as backup:

unix2dos -b [file_name]

Option 3: Using the sed Command

You can also use the sed (stream editor) command to remove the carriage return line endings. The command syntax to do so is:

sed 's/^M$//' [original_file_name]>[converted_file_name]

Instead of just typing ^M, press Ctrl+V followed by Ctrl+M to enter the carriage return symbol. When using the sed command, specify the name of the DOS file [original_file_name] and how you want to name the converted file [converted_file_name].

To change the file format from Unix to DOS, use the command:

sed 's/$/^M/' [original_file_name]>[converted_file_name]

Option 4: Using the tr Command

Another way to convert a file into the Unix format is to remove r line endings with the tr command. The tr command is a command line utility for translating or deleting characters.

Use the command in the following format:

tr -d 'r' < [original_file_name]>[converted_file_name]

Option 5: Using the Vim Text Editor

You can also remove carriage return line endings from files in DOS format using the Vi/Vim text editor.
Open the file in Vi/Vim:

vi [file_name]

Then press : and type in the following Vi/Vim command (making sure you type Ctrl+V then Ctrl+m instead of ^m):

:%s/^ m //g
Remove the carriage return line ending using the vi text editor.

Option 6: Using a Perl One Liner

Lastly, you can use a Perl one-liner command to delete all r line endings. Pearl on-liners are scripts that fit in a single line of code.

To replace all carriage return and line feed endings with just line feeds:

1. Open the file in the Vi/Vim text editor.

2. Press : to prompt the command line.

3. Type in the following command and press Enter:

perl -pi -e 's/rn/n/g' [file_name]
Delete carriage return endings using a Perl one-liner command.

You should see the the changes in the file right away.

Example: Converting a Sample File from DOS to Unix Format

Let’s say you downloaded a file named sample-dos-file.

A sample file created in Windows.

By opening the file using the Vim/Vi text editor, you would see that after each line ending there is ^M (a carriage return).

Opening a DOS file in Vi to see carrier return symbols at line endigns.

Another way to see the file uses both carriage return and line feed for line endings is to view the file in octal values.

To do so, run the command:

od -bc sample-dos-file.txt

The output displays the content of the file with its octal values, as in the image below. You can see that each line end is marked with the octal values 015 (r) and 012 (n).

See end of line marks in octal values.

Now, to convert the file and remove all carriage return endings, you would run the command:

dos2unix sample-dos-file
Convert a dos file into the Unix format.

Open the same file in Vim/Vi. It doesn’t include any ^M symbols signaling the line ending.

Open a file in the Vi text editor.

To confirm the file is in Unix format, you can also open view the content in octal values. The output should display just the 012 values for n.

Display Unix file content in octal values.

Conclusion

This article showed six ways to convert a DOS file into a Unix format and vice versa.

The simplest and recommended way is using the dos2unix command. If you cannot utilize dos2unix, you have five other options to choose from.

As a Linux administrator, you may have noticed some requests from developers to convert files from DOS format to Unix format, and vice versa.

This is because these files were created on a Windows system and copied to a Linux system for some reason.

It’s harmless, but some applications on the Linux system may not understand these new line of characters, so you need to convert them before using it.

DOS text files comes with carriage return (CR or r) and line feed (LF or n) pairs as their newline characters, whereas Unix text files only have line feed as their newline character.

There are many ways you can convert a DOS text file to a Unix format.

But I recommend using a special utility called dos2unix / unix2dos to convert text files between DOS and Unix formats.

  • dos2unix: To convert a text files from the DOS format to the Unix format.
  • unix2dos: To convert a text files from the Unix format to the DOS format.
  • tr, awk and sed Command: These can be used for the same purpose

You can easily identify whether the file is DOS format or Unix format using the od (octal dump) command as shown below.

# od -bc windows.txt
0000000 125 156 151 170 040 151 163 040 141 040 146 162 145 145 040 157
U n i x i s a f r e e o
0000020 160 145 156 163 157 165 162 143 145 040 157 160 145 162 141 164
p e n s o u r c e o p e r a t
0000040 151 156 147 040 163 171 163 164 145 155 015 012 123 165 160 145
i n g s y s t e m r n S u p e
0000060 162 040 143 157 155 160 165 164 145 162 163 040 141 162 145 040
r c o m p u t e r s a r e
0000100 162 165 156 156 151 156 147 040 157 156 040 125 116 111 130 015
r u n n i n g o n U N I X r
0000120 012 071 065 045 040 157 146 040 167 145 142 163 151 164 145 163
n 9 5 % o f w e b s i t e s
0000140 040 141 162 145 040 162 165 156 156 151 156 147 040 157 156 040
a r e r u n n i n g o n
0000160 114 151 156 165 170 040 117 123 015 012 101 156 171 164 150 151
L i n u x O S r n A n y t h i
0000200 156 147 040 143 141 156 040 142 145 040 144 157 156 145 040 157
n g c a n b e d o n e o
0000220 156 040 114 151 156 165 170 015 012
n L i n u x r n
0000231

The above output clearly shows that this is a DOS format file because it contains the escape sequence rn.

At the same time, when you print the file output on your terminal you will get the output below.

# cat windows.txt
Unix is a free opensource operating system
Super computers are running on UNIX
95% of websites are running on Linux OS
Anything can be done on Linux

How to Install dos2unix on Linux

dos2unix can be easily installed from the distribution official repository.

For RHEL/CentOS 6/7 systems, use the yum command to install dos2unix.

$ sudo yum install -y dos2unix

For RHEL/CentOS 8 and Fedora systems, use the dnf command to install dos2unix.

$ sudo yum install -y dos2unix

For Debian based systems, use the apt command or apt-get command to install dos2unix.

$ sudo apt-get update
$ sudo apt-get install dos2unix

For openSUSE systems, use the zypper command to install dos2unix.

$ sudo zypper install -y dos2unix

1) How to Convert DOS file to UNIX format

The following command converts the “windows.txt” file from DOS to Unix format.

The modification of this file is to remove the “r” from each line of the file.

# dos2unix windows.txt
dos2unix: converting file windows.txt to Unix format …
# cat windows.txt
0000000 125 156 151 170 040 151 163 040 141 040 146 162 145 145 040 157
U n i x i s a f r e e o
0000020 160 145 156 163 157 165 162 143 145 040 157 160 145 162 141 164
p e n s o u r c e o p e r a t
0000040 151 156 147 040 163 171 163 164 145 155 012 123 165 160 145 162
i n g s y s t e m n S u p e r
0000060 040 143 157 155 160 165 164 145 162 163 040 141 162 145 040 162
c o m p u t e r s a r e r
0000100 165 156 156 151 156 147 040 157 156 040 125 116 111 130 012 071
u n n i n g o n U N I X n 9
0000120 065 045 040 157 146 040 167 145 142 163 151 164 145 163 040 141
5 % o f w e b s i t e s a
0000140 162 145 040 162 165 156 156 151 156 147 040 157 156 040 114 151
r e r u n n i n g o n L i
0000160 156 165 170 040 117 123 012 101 156 171 164 150 151 156 147 040
n u x O S n A n y t h i n g
0000200 143 141 156 040 142 145 040 144 157 156 145 040 157 156 040 114
c a n b e d o n e o n L
0000220 151 156 165 170 012
i n u x n
0000225

The above command will overwrite the original file.

Use the following command if you want to keep the original file. This will save the converted output as a new file.

# dos2unix -n windows.txt unix.txt
dos2unix: converting file windows.txt to file unix.txt in Unix format …

1a) How to Convert DOS file to UNIX format Using tr Command

As discussed at the beginning of the article, you can use the tr command to convert the DOS file to Unix format as shown below.

Syntax: tr -d 'r' < source_file > output_file

The below tr command converts the “windows.txt” DOS file to Unix format file “unix.txt”.

# tr -d 'r' < windows.txt >unix.txt

Make a note: You can’t use the tr command to convert a file from Unix format to Windows (DOS).

1b) How to Convert DOS file to UNIX format Using awk Command

Use the following awk command format to convert a DOS file to a Unix format.

Syntax: awk '{ sub("r$", ""); print }' source_file.txt > output_file.txt

The below awk command converts the “windows.txt” DOS file to Unix format file “unix.txt”.

# awk '{ sub("r$", ""); print }' windows.txt > unix.txt

2) How to Convert UNIX file to DOS format

When you convert a file from UNIX to DOS format, it will add a carriage return (CR or r) in each of the line.

# unix2dos unix.txt
unix2dos: converting file unix.txt to DOS format …

This command will keep the original file.

# unix2dos -n unix.txt windows.txt
unix2dos: converting file unix.txt to file windows.txt in DOS format …

2a) How to Convert UNIX file to DOS format Using awk Command

Use the following awk command format to convert UNIX file to DOS format.

Syntax: awk 'sub("$", "r")' source_file.txt > output_file.txt

The below awk command converts the “unix.txt” file to the DOS format file “windows.txt”.

# awk 'sub("$", "r")' unix.txt > windows.txt

Понравилась статья? Поделить с друзьями:
  • List command in windows command prompt
  • List 850 плоттер драйвер windows 10
  • List 850 драйвер windows 7 64 bit
  • Linux connect to windows remote desktop
  • Linphone скачать для windows на русском