In this post i have listed common Linux commands which are very helpful for .Net developer. In most cases asp.net core developers work on Windows. Main reason is that previously .Net was only supported on Windows only, and the most helpful tool, The great IDE, Visual Studio, was only supported on windows.
Now things have changed. .Net Core is cross platform. Available for Windows, Mac and Linux. There is Visual Studio Code, available for all three platforms. However, Visual Studio is still not supported on Linux. But again, .Net core has made easy to develop .net application on Linux machine.
Now, developers who are using Linux for first time, they feel very uncomfortable switching from Windows. And things gets really DARK, when they start working on Linux server, without GUI. Everything need execute commands to perform any task. I have been through that phase. Even today I forget many Linux commands and all I do is repeated search for small Linux commands.
So, I have listed the general day to day Linux commands:
Login as root in terminal
To act as root in terminal, you can use below command:
This will convert the terminal “$” sign to “#” sign. It means now you are acting as root in terminal.
Copy & Move
To copy a file from one location to another, Here is the command:
cp firstfile secondfile
Move command is user for two purpose: Move and Rename.
So this will move first file to other location:
mv /path/to/oldfile /path/to/newfile
And this will rename the file:
mv oldfilename newfilename
The same command is used for move and rename. You can also use it to move and rename it in same command.
To list files, there is command ‘ls’. It will list all the files of the specified path. This command also takes some flags to perform other operations like sorting the listed files in a specific way. The command syntax is:
ls <path> -<flag>
For example, if you want to list all files at path /var/www/html, then:
Additionally, if you want to sort it by last modified date, then use -t flag, like:
ls /var/www/html -t
And if you add -l flag, then it will show it in the list view.
Deleting files & folders
Note: Be careful while removing files and folder, because once deleted they can not be recovered. Not like Windows bin.
To delete a folder or directory which contains other files and folders, use below command:
rm -r foldername
Here -r flag means recursively.
The same way you can delete a file.
If you want to delete multiple files, then write each file name with space between them when using this command.
In case if you want to delete all files in a folder, but not the folder itself, then use /* in the path. For example, if you want to delete all files in public_html folder for application deployment, then use:
rm -r /path/to/your/public_html/*
This will remove all files and folders in public_html folder. Note that it will not delete .htaccess kind files.
Directory Read/Write Permission
On Linux, mostly developer from windows environment face file read and write permission. It’s the case mostly with writing log files, or any file like file upload. In Linux, every user is needed to have permission to read and write file, and the process which runs the application is also act as user, that user is ‘www-data’.
Giving asp.net core (apache www-data user) write permission:
sudo chown -R www-data:www-data /var/www/example.com/public_html sudo chmod -R 770 /var/www/example.com/public_html
Running Visual Studio Code as root
Although it’s not recommended to run VS code as root, but sometimes we need to run it. For that you can this command:
sudo code --user-data-dir="~/.vscode-root"
To zip or compress any folder I use mostly ‘zip’ package. Make sure it’s installed on you machine.
To zip any folder and it’s content, you can use following command:
zip -r zippedfilename.zip folder_path_to_zip/*
Some times we need to exclude some sub-folders inside the folder we want to zip, for example, say you want to zip folder than contains nodejs based project source code, but you don’t want to include node_modules folder also zipped. So, in that case you can use following command format:
zip -r zippedfilename.zip foldername_path_to_zip -x foldername_path_to_zip/folder_to_exclude\* foldername_path_to_zip/any_other_folder_to_exclude\*
Same way you can use unzip command to unzip the folder contents.
This article is in progress, and i will update it with more commands that is used by developer.