- How to open, create, edit, and view a file in Linux
- GUI text editors
- NEdit
- Geany
- Gedit
- Terminal-based text editors
- emacs
- Redirecting command output into a text file
- Creating an empty file with the touch command
- Redirecting text into a file
- Redirecting to the end of a file
- 12 Useful Commands For Filtering Text for Effective File Operations in Linux
- 1. Awk Command
- 2. Sed Command
- 3. Grep, Egrep, Fgrep, Rgrep Commands
- 4. head Command
- 5. tail Command
- 6. sort Command
- 7. uniq Command
- 8. fmt Command
- 9. pr Command
- 10. tr Command
- 11. more Command
- 12. less Command
- If You Appreciate What We Do Here On TecMint, You Should Consider:
- file command in Linux with examples
How to open, create, edit, and view a file in Linux
One thing GNU/Linux does as well as any other operating system is give you the tools you need to create and edit text files. Ask ten Linux users to name their favorite text editor, and you might get ten different answers. On this page, we cover a few of the many text editors available for Linux.
GUI text editors
This section discusses text editing applications for the Linux windowing system, X Windows, more commonly known as X11 or X.
If you are coming from Microsoft Windows, you are no doubt familiar with the classic Windows text editor, Notepad. Linux offers many similar programs, including NEdit, gedit, and geany. Each of these programs are free software, and they each provide roughly the same functionality. It’s up to you to decide which one feels best and has the best interface for you. All three of these programs support syntax highlighting, which helps with editing source code or documents written in a markup language such as HTML or CSS.
NEdit
NEdit, which is short for the Nirvana Editor, is a straightforward text editor that is very similar to Notepad. It uses a Motif-style interface.
The NEdit homepage is located at https://sourceforge.net/projects/nedit/. If you are on a Debian or Ubuntu system, you can install NEdit with the following command:
For more information, see our NEdit information page.
Geany
Geany is a text editor that is a lot like Notepad++ for Windows. It provides a tabbed interface for working with multiple open files at once and has nifty features like displaying line numbers in the margin. It uses the GTK+ interface toolkit.
The Geany homepage is located at http://www.geany.org/. On Debian and Ubuntu systems, you can install Geany by running the command:
Gedit
Gedit is the default text editor of the GNOME desktop environment. It’s a great, text editor that can be used on about any Linux system.
The Gedit homepage is located at https://wiki.gnome.org/Apps/Gedit. On Debian and Ubuntu systems, Gedit can be installed by running the following command:
Terminal-based text editors
If you are working from the Linux command line interface and you need a text editor, you have many options. Here are some of the most popular:
pico started out as the editor built into the text-based e-mail program pine, and it was eventually packaged as a stand-alone program for editing text files. («pico» is a scientific prefix for very small things.)
The modern version of pine is called alpine, but pico is still called pico. You can find more information about how to use it in our pico command documentation.
On Debian and Ubuntu Linux systems, you can install pico using the command:
nano is the GNU version of pico and is essentially the same program under a different name.
On Debian and Ubuntu Linux systems, nano can be installed with the command:
vim, which stands for «vi improved,» is a text editor used by millions of computing professionals all over the world. Its controls are a little confusing at first, but once you get the hang of them, vim makes executing complex editing tasks fast and easy. For more information, see our in-depth vim guide.
On Debian and Ubuntu Linux systems, vim can be installed using the command:
emacs
emacs is a complex, highly customizable text editor with a built-in interpreter for the Lisp programming language. It is used religiously by some computer programmers, especially those who write computer programs in Lisp dialects such as Scheme. For more information, see our emacs information page.
On Debian and Ubuntu Linux systems, emacs can be installed using the command:
Redirecting command output into a text file
When at the Linux command line, you sometimes want to create or make changes to a text file without actually running a text editor. Here are some commands you might find useful.
Creating an empty file with the touch command
To create an empty file, it’s common to use the command touch. The touch command updates the atime and mtime attributes of a file as if the contents of the file had been changed — without actually changing anything. If you touch a file that doesn’t exist, the system creates the file without putting any data inside.
For instance, the command:
The above command creates a new, empty file called myfile.txt if that file does not already exist.
Redirecting text into a file
Sometimes you need to stick the output of a command into a file. To accomplish this quickly and easily, you can use the > symbol to redirect the output to a file.
For instance, the echo command is used to «echo» text as output. By default, this goes to the standard output — the screen. So the command:
The above command prints that text on your screen and return you to the command prompt. However, you can use > to redirect this output to a file. For instance:
The above command puts the text «Example text» into the file myfile.txt. If myfile.txt does not exist, it is created. If it already exists, its contents will be overwritten, destroying the previous contents and replacing them.
Be careful when redirecting output to a file using >. It will overwrite the previous contents of the file if it already exists. There is no undo for this operation, so make sure you want to completely replace the file’s contents before you run the command.
Here’s an example using another command:
The above command executes ls with the -l option, which gives a detailed list of files in the current directory. The > operator redirects the output to the file directory.txt, instead of printing it to the screen. If directory.txt does not exist, it is created first. If it already exists, its contents will be replaced.
Redirecting to the end of a file
The redirect operator >> is similar to >, but instead of overwriting the file contents, it appends the new data to the end of the file. For instance, the command:
Источник
12 Useful Commands For Filtering Text for Effective File Operations in Linux
In this article, we will review a number of command line tools that act as filters in Linux. A filter is a program that reads standard input, performs an operation upon it and writes the results to standard output.
For this reason, it can be used to process information in powerful ways such as restructuring output to generate useful reports, modifying text in files and many other system administration tasks.
With that said, below are some of the useful file or text filters in Linux.
1. Awk Command
Awk is a remarkable pattern scanning and processing language, it can be used to build useful filters in Linux. You can start using it by reading through our Awk series Part 1 to Part 13.
Additionally, also read through the awk man page for more info and usage options:
2. Sed Command
sed is a powerful stream editor for filtering and transforming text. We’ve already written a two useful articles on sed, that you can go through it here:
The sed man page has added control options and instructions:
3. Grep, Egrep, Fgrep, Rgrep Commands
These filters output lines matching a given pattern. They read lines from a file or standard input, and print all matching lines by default to standard output.
Note: The main program is grep, the variations are simply the same as using specific grep options as below (and they are still being used for backward compatibility):
Below are some basic grep commands:
4. head Command
head is used to display the first parts of a file, it outputs the first 10 lines by default. You can use the -n num flag to specify the number of lines to be displayed:
Learn how to use head command with tail and cat commands for effective usage in Linux.
5. tail Command
tail outputs the last parts (10 lines by default) of a file. Use the -n num switch to specify the number of lines to be displayed.
The command below will output the last 5 lines of the specified file:
Additionally, tail has a special option -f for watching changes in a file in real-time (especially log files).
The following command will enable you monitor changes in the specified file:
Read through the tail man page for a complete list of usage options and instructions:
6. sort Command
sort is used to sort lines of a text file or from standard input.
Below is the content of a file named domains.list:
You can run a simple sort command to sort the file content like so:
You can use sort command in many ways, go through some of the useful articles on sort command as follows:
7. uniq Command
uniq command is used to report or omit repeated lines, it filters lines from standard input and writes the outcome to standard output.
After running sort on an input stream, you can remove repeated lines with uniq as in the example below.
To indicate the number of occurrences of a line, use the -c option and ignore differences in case while comparing by including the -i option:
Read through the uniq man page for further usage info and flags:
8. fmt Command
fmt simple optimal text formatter, it reformats paragraphs in specified file and prints results to the standard output.
The following is the content extracted from the file domain-list.txt:
To reformat the above content to a standard list, run the following command with -w switch is used to define the maximum line width:
9. pr Command
pr command converts text files or standard input for printing. For instance on Debian systems, you can list all installed packages as follows:
To organize the list in pages and columns ready for printing, issue the following command.
The flags used here are:
- —column defines number of columns created in the output.
- -l specifies page length (default is 66 lines).
10. tr Command
This tool translates or deletes characters from standard input and writes results to standard output.
The syntax for using tr is as follows:
Take a look at the examples below, in the first command, set1( [:upper:] ) represents the case of input characters (all upper case).
Then set2([:lower:]) represents the case in which the resultant characters will be. It’s same thing in the second example and the escape sequence \n means print output on a new line:
11. more Command
more command is a useful file perusal filter created basically for certificate viewing. It shows file content in a page like format, where users can press [Enter] to view more information.
You can use it to view large files like so:
12. less Command
less is the opposite of more command above but it offers extra features and it’s a little faster with large files.
Use it in the same way as more:
Learn Why ‘less’ is Faster Than ‘more’ Command for effective file navigation in Linux.
That’s all for now, do let us know of any useful command line tools not mentioned here, that act as a text filters in Linux via the comment section below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник
file command in Linux with examples
file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it.
It has three sets of tests as follows:
- filesystem test: This test is based on the result which returns from a stat system call. The program verifies that if the file is empty, or if it’s some sort of special file. This test causes the file type to be printed.
- magic test: These tests are used to check for files with data in particular fixed formats.
- language test: This test search for particular strings which can appear anywhere in the first few blocks of a file.
Syntax:
Example: Command displays the file type
Options:
- -b, –brief : This is used to display just file type in brief mode.
Syntax:
Example:
Here, we can see that file type without filename.
* option : Command displays the all files’s file type.
The output shows all files in the home directory
directoryname/* option : This is used to display all files filetypes in particular directory.
Syntax:
Example:
The output shows all files in a particular directory.
[range]* option: To display the file type of files in specific range.
Syntax:
Example:
The output shows the range of files.
-c option: Cause a checking printout of the parsed form of the magic file. This option is usually used in conjunction with the -m flag to debug a new magic file before installing it.
Example:
-f option: Read the names of the files to be examined from namefile (one per line) before the argument list. Either namefile or atleast one filename argument must be present; to test the standard input, use ‘-’ as a filename argument.
Syntax:
-F option : File and file type are separated by :. But we can change separator using -F option
Syntax:
Example:
The output shows file and file types are separated by – and +.
-i option: To view mime type of file.
Syntax:
Example:
-N option: Don’t pad filenames so that they align in the output.
Example:
-s option: For special files
Syntax:
Example:
filenames: Displays file types of multiple files
Syntax:
Example:
-z option: Try to look inside compressed files.
Example:
–help option: Print a help message and exit.
Источник