- Use CAT Command to Combine Text Files in Ubuntu 18.04
- Example 1: Merging text from three files to another text file
- Example 2: Merging text from three files, and saving the output to another file in alphabetical order
- Example 3: Appending text from one text file to another
- Example 4: Appending text from the Terminal directly to a file
- Karim Buzdar
- How To Split And Combine Files From Command Line In Linux
- Split And Combine Files From Command Line In Linux
- Split files in Linux from command line
- Combine files in Linux from command line
- Conclusion
- How to create multiple files with the Terminal?
- 4 Answers 4
- Explanation:
- Create correctly numbered (next) file with a shortcut key combination
- How to use
Use CAT Command to Combine Text Files in Ubuntu 18.04
The CAT command in Linux is not only helpful in creating text files, displaying their contents, but also in merging text from two or more text files. The merged text can then be saved to another text file. In this tutorial, you will learn the use of the CAT command to combine text from two or more files into a single one. This will help you in achieving a power user status on Ubuntu from an average beginner. We have run the commands mentioned in this tutorial on an Ubuntu 18.04 LTS system.
Let us present a few examples in this article which will help you in understanding the proper usage of the CAT command in the following four scenarios:
- Merging text from multiple text files to a single text file.
- Merging text from multiple files, and saving the output to another file in alphabetical order.
- Appending text from one text file to another.
- Appending text from the Ubuntu Terminal directly to a text file.
Note: It is a good practice to backup important files before altering their contents.
Example 1: Merging text from three files to another text file
We have created three sample text files on our system by the name of textfile1.txt, textfile2.txt, and textfile3.txt. All of these files contain a line of text. The following use of the CAT command will display the text from all of these files in a single output.
Open the Ubuntu Terminal by either pressing CTRl+Alt+T or through the Dash, and then enter the following command:
In the following image you can see how the output from my three text files is printed as a single merged output:
Linux allows you to print the output of a command to a file by using the following syntax:
Let us make use of this command and the cat command to save the text from three different text files to a new text file:
In the following image, I am saving the merged text from my three files to a new file textfile4.txt; I am then printing the contents of the new file to the screen for you to view:
Please remember that if the destination text file already exists in your system, its contents will be overwritten.
Example 2: Merging text from three files, and saving the output to another file in alphabetical order
Suppose you have three text files; each containing some text. You want to merge the text from all three and save the output to a fourth file, but in alphabetical order. This is how you will do it: Advertisement
In the following image, you can view the text from each of my text files. If I simply combine the text to a new file textfile4.txt, the output will be as follows:
However, I want an alphabetically sorted output to be printed to my text file, so I will use the following command:
You can see how my newly created textfile5.txt contains merged and sorted text from my three source files.
Example 3: Appending text from one text file to another
The cat command can also be used to append text from a source file to a destination file without messing up with the contents of the later.
Here is a sample destination file:
Here is a sample source file:
The syntax for appending text:
Here is how my destination file looks after I append the text from my source file to it:
Example 4: Appending text from the Terminal directly to a file
If you want to append some text, from the command line, at the end of an already existing text file, you can use the following syntax:
After entering this command, a cursor will appear for you to enter the text you want to add to the specified file. Enter the text and press Ctrl+D. Your entered text will be appended at the end of the file without disturbing its already existing contents.
You can see this text added to the file in the following image:
We hope that the detailed examples described in this article along with the syntax of the cat command in each case will help in merging the contents of multiple files into a single one. Moreover, you can excel sorting and appending of text not only from one file to another but also directly from the Ubuntu Terminal.
Karim Buzdar
About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn
Источник
How To Split And Combine Files From Command Line In Linux
In this brief tutorial, I will show how to split and combine files from command line in Linux and Unix-like operating systems using «split» and «cat» commands.
I have a lot of Linux tutorial videos in my Linux desktop and I wanted to move all of them to my Google Drive. The problem is the size of some video files is more than 1 GiB. It is practically not a good approach to upload such big files to the Google drive. Even though Google Drive allows us to upload files up to 5TB, I find it is very time consuming process to upload all big files.
While contemplating with this issue, I thought it would be much better if I can be able to split those files into smaller size and upload them one by one. I can, then, download all parts of the file, and combine all of them, whenever I want. This is just an example. You might have different reasons to break a large file into multiple pieces and combine them later. If you ever been in this situation, afraid not. There is a simple command line utility called «split» which is used to split the big files into multiple smaller files. You can join all of them later to make a single file.
Split And Combine Files From Command Line In Linux
First, let us see how to split a big file inti multiple smaller files.
Split files in Linux from command line
Have a look at the size of the following video file.
Sample output:
As you see, the video file size is 1.1 GiB, which is very large to upload to my google drive. Even though, Google Drive lets users upload files up to 5TB in size, it is really time consuming process. With my low speed Internet connection, I presume it would take more than 30 minutes to upload. I don’t want to wait that much longer. So, what I am going to do is split this file into multiple smaller size files, for example 100 MB each, to make upload process faster.
Now, let us split the above file into multiple smaller files, say for example 100MB each. To do so, run:
The above command splits the Linux Security.mp4 file into 100MB chunks. This command creates files named ls.aa, ls.ab, ls.ac.. and so on.
Let us take a look at the output after splitting the large file.
Sample output:
See? Linux Security.mp4 file has been split into multiple files named ls.aa, ls.ab . ls.ak etc. Each file size is 100MB.
Now, It is little bit easy to send them as Email attachment.
Combine files in Linux from command line
Save all files in a folder. And then, go to that folder and combine them as follows.
Here, Linux_security.mp4 is output file name. The double question marks(??) match any two-character extension in the file name. To put this simply, the filename part ls. matches all filenames such as ls.aa, ls.ab ..etc., and combine all of them into single file. Please be mindful that this command will combine all files that contains two-character extensions. So, be sure before combining files. If there are some other files with two letter extensions, they will also be merged into the output file.
Also, don’t forget to mention the correct extension while merging them. In case, you want to send all files via mail to your friend, tell him/her the correct extension of the files you have sent. He/she should use the same file extension in the output file while combining them.
If you don’t specify any argument in the split command, the file will split into multiple smaller files with x as file names. Each file would contain 1000 lines by default. Here, is the two character suffix that is added by default with each file name.
Let us split the same file using split command without specifying any extra arguments like below.
This command splits the Linux security file into multiple smaller files with x** as file names.
Run ‘ls’ command to view the files:
Each file should contain 1000 lines. You can also verify it using wc (word count) command as shown below.
Sample output would be:
To combine all these files, run:
The above command will combine all files into a single file called ls.mp4.
For more details, refer man pages.
Recommend Read:
Conclusion
There could be other CLI and GUI tools available in Linux to split or combine files. But, ‘split’ is built-in command that comes pre-installed. So, don’t bother installing any additional tools on your Linux box. Also, split command breaks the file into multiple pieces quickly.
Источник
How to create multiple files with the Terminal?
I’ve been learning to program with a really cool introductory book to C and I write off every example and task so I can learn the syntax by heart. I did every file manually until now, but the clicking, naming and saving gets exhausting.
I need a way to create multiple files like bspl0001.c , bspl0002.c , bspl0003.c , etc. and saving them in directory «learning_c» or something.
I’m a noob with Ubuntu / Linux and honestly only run it via VirtualBox because the IDE Geany was easy to set up. Thus, I don’t really know how the Terminal works.
edit I’ve just found myself in this old account of mine and am shaking my head about why I would ever want to learn a code syntax by heart. Funnily enough, I am almost done with my CS / Math BSc studies. Thanks much for the help in hindsight!
4 Answers 4
You can do this with these commands:
Explanation:
- This will create a folder called learning_c in the current folder
- The current folder usually is your home folder also called
- Yes, you can guess it, you’re entering on the newly created folder
- touch is a tool to create empty files and modify timestamps; we’re creating empty files.
- touch myfile will create an empty file called myfile .
The ugly code that follows ( bspl<0001..0003>.c ) is called a brace expansion. This is a great feature of the bash shell that allows you to create long lists of arbitrary string combinations. You can learn more about this in the Bash Hackers Wiki. In this case you will be making a long list of parameters that will be passed to touch . You can also use its long equivalent:
You can change the number of files: if you want 12 files, you can run bspl<0001..0012>.c .
You can use the following python code, you can modify it to fit your needs.
Save the following code with filename filecreator.py
And then execute it with this command:
Create correctly numbered (next) file with a shortcut key combination
Why create all files at once? The disadvantage is that you will have a lot of empty and unused files. What I am actually using: press a key combination to:
- have a script see in my code directory what should be the «next» file,
- create the correctly named file (including shebang) and
- open the new file in my editor (in my case Idle).
All in one keypress. That way you prevent a lot of (still) unused files; The files are only created if you need them.
A simplified version below (not running step 3). On every keypress, it will create a correctly numbered file like:
How to use
- Copy the script into an empty file
- In the head section, set the path to your directory (and optional: change the base name and/or extension, shebang).
- Save the script as create_empty.py
Run the script from a shortcut: System Settings > Keyboard > Custom Shortcuts. Add the command:
The bash way is good, but what if you are working with a shell that doesn’t support the curly brace expansion ? touch file <1..10>doesn’t work for me on mksh for instance. Here’s three alternative ways that work regardless of the shell.
A more shell-neutral approach would be to combine seq command to generate sequence of numbers formatted with printf options , and pass it to xargs command. For example,
Of course, Perl , being quite a widespred *nix tool, can do that as well. The specific one-liner command that we have here is the following:
Effectively what happens here is that we specify 3 command-line arguments: filename prefix, starting index, and ending index. Then we use do < >for $ARGV[1] .. $ARGV[2] to iterate for a specific range of numbers. Say, $ARGV[1] was 5 and $ARGV[2] was 9, we would iterate over 5,6,7,8 and 9.
What happens on each iteration within the curly braces ? we take each number specified with $_ , and using sprintf() function create a string m which splices up the prefix (first command-line argument, $ARGV[0] ) and the given number, but filling up the number with 4 zeros (which is done by printf -style of formatting , %04d part), and attach the .c suffix.As a result on each iteration we make up a name like bspl0001.c .
The open(my $fh, «>», $var);close($fh) effectively acts as touch command, creating a file with specified name.
While slightly lengthy it performs quite well, in fashion similar to Jacob Vlijm’s python script. It can also be converted to a script for readability if desired, like so:
Lets test this. First the one-liner:
And now the script:
Another approach would be with awk , running a for loop, redirecting to a specific file. The approach is similar to the perl one-liner with command-line arguments. While awk is primarily a text processing utility, it still can do some cool system programming.
Источник