How started file in linux

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.

Читайте также:  Linux как настроить удаленный рабочий стол

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:

Источник

5 Commands to View the Content of a File in Linux Command Line

If you are new to Linux and you are confined to a terminal, you might wonder how to view a file in the command line.

Reading a file in Linux terminal is not the same as opening file in Notepad. Since you are in the command line mode, you should use commands to read file in Linux.

Don’t worry. It’s not at all complicated to display a file in Linux. It’s easy as well essential that you learn how to read files in the line.

Here are five commands that let you view the content of a file in Linux terminal.

5 commands to view files in Linux

Before you how to view a file in Unix like systems, let me clarify that when I am referring to text files here. There are different tools and commands if you want to read binary files.

1. Cat

This is the simplest and perhaps the most popular command to view a file in Linux.

Cat simply prints the content of the file to standard display i.e. your screen. It cannot be simpler than this, can it?

cat displays the content of the file on the screen

Cat becomes a powerful command when used with its options. I recommend reading this detailed tutorial on using cat command.

The problem with cat command is that it displays the text on the screen. Imagine if you use cat command with a file that has 2000 lines. Your entire screen will be flooded with the 200 lines and that’s not the ideal situation.

So, what do you do in such a case? Use less command in Linux (explained later).

The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.

nl command displays text with line numbers

There are a few options with nl command that allows you to control the numbering. You can check its man page for more details.

3. Less

Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen. Your terminal remains clean and pristine.

I strongly recommend learning a few options of the Less command so that you can use it more effectively.

There is also more command which was used in olden days but less command has more friendly features. This is why you might come across the humorous term ‘less is more’.

4. Head

Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.

You can change this behavior by using options with head command but the fundamental principle remains the same: head command starts operating from the head (beginning) of the file.

5. Tail

Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.

Читайте также:  Creative sb0220 windows 10 x64 driver

By default, tail command displays the last 10 lines of a file.

Head and Tail commands can be combined to display selected lines from a file. You can also use tail command to see the changes made to a file in real time.

Bonus: Strings command

Okay! I promised to show only the commands for viewing text file. And this one deals with both text and binary files.

Strings command displays the readable text from a binary file.

No, it doesn’t convert binary files into text files. If the binary file consists of actual readable text, strings command displays those text on your screen. You can use the file command to find the type of a file in Linux.

Conclusion

Some Linux users use Vim to view the text file but I think that’s overkill. My favorite command to open a file in Linux is the less command. It leaves the screen clear and has several options that makes viewing text file a lot easier.

Since you now know ways to view files, maybe you would be interested in knowing how to edit text files in Linux. Cut and Paste are two such commands that you can use for editing text in Linux terminal. You may also read about creating files in Linux command line.

Источник

How To Run the .sh File Shell Script In Linux / UNIX

I have downloaded software for my Linux or Unix-like system from the Internet. There is a file called install.sh. How do I run an .sh file to install the software in macOS? How do your run .sh files from command line?

You can open or run .sh file in the terminal on Linux or Unix-like system. The .sh file is nothing but the shell script to install given application or to perform other tasks under Linux and UNIX like operating systems. The easiest way to run .sh shell script in Linux or UNIX is to type the following commands. Open the terminal (your shell prompt) and type the commands.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix-like systems
Est. reading time 3 mintues

How do I run .sh file shell script in Linux?

The procedure to run the .sh file shell script on Linux is as follows:

  1. Open the Terminal application on Linux or Unix
  2. Create a new script file with .sh extension using a text editor
  3. Write the script file using nano script-name-here.sh
  4. Set execute permission on your script using chmod command :
    chmod +x script-name-here.sh
  5. To run your script :
    ./script-name-here.sh
    Another option is as follows to execute shell script:
    sh script-name-here.sh
    OR bash script-name-here.sh

Let us see script examples and usage in details.

Syntax

The syntax is:
sh file.sh
OR
bash file.sh

How to run .sh file as root user

Some time you need root access to install application; without root, you won’t have the necessary permissions to install application or make system level modifications. Root access is disabled by default on many Linux and UNIX like systems. Simply use the sudo or su command as follows:
sudo bash filename.sh
Type your password. Another option is to use the su command as follows to become superuser:
su —
Type root user password and finally run your script:
bash filename.sh

How to use chmod command to run .sh shell script in Linux

Another recommend option is to set an executable permission using the chmod command as follows:
chmod +x file.sh
Now your can run your .sh file as follows
./file.sh
/path/to/file.sh

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How do I run .sh file named install.sh?

Just run the following two command:
chmod +x install.sh
./install.sh
The dot (.) refers to the current working directory. The install.sh is in the current directory so you run it as above or as follows too:
bash install.sh
OR
sh install.sh

GUI method to run .sh file

  1. Select the file using mouse.
  2. Right-click on the file.
  3. Choose Properties:
  4. Click Permissions tab.
  5. Select Allow executing file as a program:
  6. Now click the file name and you will be prompted. Select “Run in the terminal” and it will get executed in the terminal.

Running .sh file shell script and debugging options

Pass the -x to debug shell script when running on your system. It print commands and their arguments as they are executed. For example:
bash -x script-name.sh
sh -x script-name.sh
Want to show shell input lines as they are read. Try passing the -v option:
bash -v script-name
We can combine both options. In other words, try it as follows:
bash -x -v backup.sh
For more information see how to improve your bash/sh shell script with ShellCheck lint script analysis tool and other bash debugging hints here.

Conclusion

You learned how to run .sh file shell script using combination of the chomod and dot (.) or sh/bash command. We can use any one of the following command execute shell script on Linux and Unix-like systems:

See the following resources for more info:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

anybody tell by which coomand i will check the modified date of files/dir in linux o/s

date -s “2 OCT 2006 18:00:00”

if we want to install software from cd.. how should be the command? i’m the newbie

rashid, if you want to install software from the cd you should try synaptic package handler or apt-get .

if the software is usually from the cd it will tell you to insert it

How can i execute this command export PS1=”\[[33[01;32m\]\u@\h\[33[01;34m\] \W]\]#” from a .sh script for example and make it work ?

Hey Black_Ps`
There are a few ways to do this but if you are not familiar with some of the nuances of Linux, it might be easier to create a new folder to place the file in (or all of your scripts in) like home/username/Desktop/scripts (substituting the actual user name in place of username). The following instructions are based on Fedora 10 with GNOME desktop so if you are running a different Linux distro, you may or may not have a few slight variances. Once you have a good place to store your scripts, you can use the GUI to help with creation of the shell script. Next, click on the “Applications” menu and highlight “Accessories” and then select “Text Editor” Type the following command #!/bin/bash and then press the enter key. Now, type the command you listed into the text editor and then select the “Save As” option. Now name your file (highly recommended that you don’t name it with spaces and make the name all lower case to help with ease of use later) and save it to your new folder you created…remember to give it a .sh name extension. Now close the text editor and go back to the “Applications” menu and highlight “System Tools” and then select “Terminal”. Type the following command cd /home/username/Desktop/scripts (again substituting the actual user name in place of username). The command ls and then press the enter key. Did it return the name of your script as a file in that folder? If so, move on to the next step. If it did not, navigate to the folder where you saved this file and move it to the correct folder. Type the following command chmod 755 yourfilename.sh. That’s it, you now have an executable shell script that will execute your command. You can do so by double clicking on it from the GUI, calling it from the terminal like so bash /home/username/Desktop/scripts/yourfilename.sh or you could put it on the cron to run unattended if you needed to. I hope this helps some or at least points you in the right direction.

can you please expalin it more detailes.

Ok thanks i’ll give it a try

When i open the terminal in UNIX..something like interactive keyboard authentication bla.. bla.. is coming..nd its difficult me to go on typing my commands and navigating through the directors and files..how can i fix this. any idea…? any command? anything helpful will be highly appreciated. _thanks_

Источник

Читайте также:  Лучший чистильщик для windows 2021
Оцените статью