This tutorial is the second part of the introduction to Linux command-line. In the first chapter I tried to make a brief introduction to the shell and the basic ways of moving around. Here I will talk about several useful commands, which may be needed on a day to day basis if you're working with the shell.
Working with Files and DirectoriesCreating DirectoriesThe command to create new directories (also called folders) is mkdir. For example, to create a directory called booksinside your home directory, you would issue the following command: mkdir books Renaming FilesIn Linux, renaming a file is done via the mvcommand, which stands for 'move'. The general format of the command is: mv SOURCE DESTINATION So, if for example you would like to rename the directory created previously, books to mybooks2, you would issue the following command: mv books mybooks2 Copying FilesThe command for copying files and directories is cp. For example, to copy the file /etc/network/interfaces to you home directory you could write the following: cp /etc/network/interfaces /home/USER Make sure to replace USER with your username. Or, if we keep in mind that ~is automatically expanded into the home directory of the current user: cp /etc/network/interfaces ~ And if your current working directory is already your home directory (e.g. /home/USER): cp /etc/network/interfaces . The character .expands into the current directory. See the first chapter to see more details on this. You could even use environment variables, which expand into a certain string. In the following example, the variable $HOME holds the path to your home directory: cp /etc/network/interfaces $HOME You may have noticed that if you copy a file to a path that ends in a directory (e.g. /home/USER), the file will be copied inside the respective directory, it will not delete nor replace it. You can copy more than one file at a time, the last argument being the directory where you want the files to be copied: cp myfile1 myfile2 myfile3 ~/books This command will copy myfile1, myfile2 and myfile3 from the current working directory into the booksfolder, located inside your home path. Copying DirectoriesDirectories can be copied in the same fashion as files, only this time we will use the -r switch. This tells the cpcommand to copy directories recursively, meaning all the sub-folders and files of the specified directory. cp -r Documents ~/books This command will copy the Documents folder from the current directory inside the booksdirectory located in your home path. Creating FilesTo create empty files, you can use the touchcommand. In a later chapter I will discuss on how to create and edit or modify text files with various editors. touch myfile Removing FilesTo delete a file, you would use the rmcommand: rm myfile To delete a directory, you will also need the -rswitch: rm -r mybooks Adding UsersAdding a New UserThe command is adduser which is a user-friendly wrapper for the useraddcommand. More on administrating users and groups in a later chapter. Other CommandsView the Current Date and TimeUse the datecommand for displaying the date and time: $ date Thu Oct 2 16:32:58 EEST 2014 In addition, you can use format specifiers to display the date and time differently. For example, you could use the %Y (year), %b (abbreviated month name), %d (number of the day, two digits), %H (hour, 24-hour format), %M (minute), %S (second): $ date +"%Y %b %d %H:%M:%S" 2014 Oct 02 16:58:44 To change the date and time, use the --setswitch: sudo date +"%H%M" --set="1656" The above command will change the hour and minute to 16:56. CalendarThe calcommand will display an ASCII calendar of the current month, and will highlight the current day: ![]() Show Your UsernameThe whoami command will print the username currently logged in. Additionally, you can use the echo $USERcommand, which will use the $USER environment variable to echo (print) its value: ![]() Print System InformationFor this we will use the uname command with the -aswitch, to show all the information available: $ uname -a Linux mint 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:31:42 UTC 2014 i686 i686 i686 GNU/Linux This shows the kernel version, the date and processor architecture. Except for uname, there are two other commands which print hardware information, lspci which prints all the PCI devices, and inxi, which is a script usually available on Linux Mint systems. $ inxi CPU~Dual core Intel Celeron CPU G530 (-MCP-) clocked at 1600.000 Mhz Kernel~3.13.0-24-generic i686 Up~2 days Mem~824.3/3721.2MB HDD~500.1GB(68.9% used) Procs~189 Client~Shell inxi~1.8.4 inxidisplays extended information about the CPU, kernel, memory, hard-disk space and so on. Show UptimeThe uptimecommand is used to print for how long the system has been running: $ uptime 17:21:25 up 2 days, 21:02, 7 users, load average: 0.28, 0.21, 0.21 Print TextThe echocommand is used to output text: $ echo "Hello, world!" Hello, world! You can use the echo -ecommand to interpret special characters (like in C): $ echo -e "Hello,\t world\!\n\n" Hello, world\! Here we used a horizontal tab (\t), and two newlines at the end (\n). Notice how the !character is escaped too. Searching for CommandsCommands and programs usually reside inside the /bin, /sbin and /usr/bin directories. To search for a command in such standard locations, use whereis: $ whereis cal cal: /usr/bin/cal /usr/bin/X11/cal /usr/share/man/man1/cal.1.gz To search for a command in all the possible locations in your $PATH, use which: $ which cfgr.sh /floydb/debconf/scripts/cfgr.sh Environment variables such as $HOME, $USER or $PATH are variables which will be expanded (replaced) by the shell with the string that they hold. For example, the $PATH variable contains all the locations in which commands reside, separated by the : character. Try echo $PATH to see it. Show File TypesUse the typecommand for this: $ file track01.ogg track01.ogg: Ogg data, Vorbis audio, stereo, 44100 Hz, ~192000 bps, created by: Xiph.Org libVorbis I (1.1.0 RC1) $ file cover.png cover.png: PNG image data, 320 x 317, 8-bit/color RGBA, non-interlaced Getting HelpUsually commands have a manual page, so for any command you could use man COMMANDto read a detailed help about what it does and how to use it. The manual page lists all the parameters you can pass to the command. Once you've opened a manual page, you can navigate through it via the arrows, Ctrl+P or Ctrl+N to read the previous and respectively, next line, or Q to quit and close the manual page and return to the shell. Commands usually have a -h or --help parameter, which will display a short description on what the command does and a compact list of parameters that it takes. More on getting help in a following chapter. |
News
Thursday, October 02, 2014
CLI Intro: Useful General-Purpose Commands
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2014
(407)
-
▼
October
(192)
- Windows Phone Updated with Evening Reminders for C...
- Microsoft Global Readiness: Diverse cultures. Many...
- Microsoft Launches Azure Cloud Services In Austral...
- Monday, October 27, 2014Xbox One : nouvelle baisse...
- Microsoft now giving Office 365 users unlimited st...
- Microsoft brings Black Friday early with another X...
- An Offer You Can't Refuse? All Office 365 Subscrib...
- Storage Wars: Microsoft offers unlimited OneDrive ...
- Microsoft’s Bing beats Google to emoji search
- Microsoft Just Made Its Google Apps Killer Much Mo...
- NYC has been struck by first case of Ebola
- How long can Ebola live on a surface?
- WHO sends medics to Mali; moves up Ebola vaccine t...
- After 1st Ebola case in NYC, 3 others quarantined
- NY governor: Doctor had 100.3 temp, not 103
- Doctors Without Borders: Ebola risk can't be zero
- Retracing the Steps of Doctor Who Tested Positive ...
- Obama offers federal help to NY with Ebola case
- NYC man diagnosed with Ebola amid official reassur...
- New Linux Releases from Ubuntu and SuSE
- New Linux Releases from Ubuntu and SuSE: In the sa...
- How does Linux determine filename case on ISO 9660?
- Linux - select() call not returning when I expect
- Very Large Linux Distribution Performance Comparis...
- IC Software: Validation Engineer - Linux / Android...
- Troubleshooting INET Routing - Linux IPsec (Racoon...
- YiKai Chen replied to Secure setting issue with Ne...
- Linux VPS Hosting - CentOS, Ubuntu, or Debian|Fast...
- U2 album stumbles in Billboard chart after free iT...
- Early Apple computer sells for $905,000 at auction
- Apple computer sells for record $905K in NY: Bonhams
- Christian Bale to play Apple's Steve Jobs
- Apple's iPad Air 2 has anti-reflective coating, a ...
- Apple's new iPad Air starts at $499
- iPhone rollout likely widened T-Mobile's growth lead
- APPLE LIVE: iPad Air 2, Apple Pay, new Macs debut
- New Apple iPad can switch wireless carriers with a...
- Patient isolated at Massachusetts clinic after dis...
- From ebola phone call to the golf course — click t...
- Texas Health Worker Tests Positive for Ebola
- Ebola Response: Maining one ebola free area in Sei...
- Health IT: Ebola technology survey
- Ebola Response: How can we Spiked M. tuberculosis ...
- Ebola Response: Maintaining one ebola free area in...
- DOH rolls out multi-sectoral response plan versus ...
- CBS Medical Expert Praises Ebola Response, Discour...
- Breach in Protocol Led to New Ebola Diagnosis: CDC
- Texas health worker with Ebola wore full protectiv...
- Second US Ebola case raises questions about safety...
- Dallas Health Care Worker Tests Positive For Ebola
- ABC Chief Medical Expert: CDC Wrong, All US Hospit...
- Suspected Ebola case isolated at Braintree clinic
- The Extremely Remote Accuracy of Ebola Predictions
- Scientists in Russia developing three Ebola vaccin...
- 2nd Ebola case in Dallas: Nurse tests positive
- NIH Official: ‘The System Worked’ on Ebola
- Second case of Ebola in Texas
- At Least 100,000 Snapchat Photos Hacked
- v1: Ebola screening for NHS 111 calls announced
- Snapchat videos and pictures stored on a third par...
- Get up to speed on Ebola outbreak
- SNAPCHAT LEAK Hackers get their hands on 100K 'del...
- More Than 200,000 Snapchat Accounts Hackeds
- There is still uncertainly over whether there will...
- SnapChat Pics Leaked From Hacked Archive
- Snapchat is the latest service to have images leak...
- Snapchat leak affects tens of thousands of people,...
- Snapchat photo leak: Company says its servers are ...
- ICYMI: Still trust Snapchat?
- skypestripper: snapchat must be fun for hot people
- Snapchat hacked
- Privacy Breach: Hackers Plan To Leak Thousands Of ...
- Latest Snapchat photo leak shows why you don’t use...
- Snapchat Warning....
- Illinois Official Warns of Ebola Email Scam
- Ebola Death Toll Eclipses 4,000 With No Signs of S...
- Return of the Android SMS virus - self-spreading "...
- Police thwarted by remote wiping of tablets and ph...
- FBI's warrantless 'hack' of Silk Road was legal, p...
- Reminder: iCloud's going to demand app-specific pa...
- Adobe will update e-reader to mop up clear-text da...
- Chinese workers strike at Foxconn factory after HP...
- Omron image sensors designed to be 'eyes' of IoT
- SeaMonkey 2.30 Beta 1
- Python 3.4.2
- Wireshark 1.99.0 (32-bit) Beta
- Sandra Lite 2014 SP3e (20.50)
- Dropbox 2.10.39
- Twitter sues US federal agencies in attempt to rem...
- Any Video Converter 5.7.3
- Sharp, NHK push 8K TVs ahead of 2020 Olympics
- Google asks Supreme Court to decide Oracle's Andro...
- Judges spar with attorneys on national security da...
- Four-digit passcodes remain a weak point in iOS 8 ...
- Enterprise mobile messaging market gets more crowd...
- The Zuli Smartplug shrinks geo-fencing to room-fen...
- Jitters over US surveillance could break the Inter...
- Passpoint Wi-Fi system makes it even easier to roa...
- Sprint's long, rocky WiMax journey will end next N...
- AMD names Lisa Su to replace Rory Read as CEO, con...
-
▼
October
(192)
No comments:
Post a Comment