1 (edited by brad 2016-08-12 10:15:14)

Hello everyone. I thought a tips and tricks thread on command line commands you either like or find very useful would be quite an interesting thread.

I only have a windows machine so do not know of any other command line tools apart from the one I use. This means that I cannot tell you about those commands. Perhaps someone on this forum who uses other operating systems with the command line can tell us about how they enjoy using it.

I am in no way an advanced command line user, in fact, I only know the basics.

so I'm going to start us off with the delete function.

Name: Delete command/function.

What does it do?

This command/function allows you to delete files.

How do I use this command/function?

1. CD in to the directory you want to delete the files from.
2. Write the following: Del space and then the name of the file you want to delete and press enter.

Note 1: Remember, you must write the full name of a file, including its extention. For example: book1.mp3.

Note2: There are switches you can use with the slash character after the del command but I haven't used them so am unsure as to what they would do.

Exciting tip! Guess what? You can delete a lode of files within a folder that have the same extention by using the folllowing:

1. CD into the directory you want to delete the files from.
2. Write the following: Del space star dot mp3 for example and press enter. Poof! All the MP3 files will be gone! This is a very useful command as it is very quick and very easy to use once you get the hang of it.

Note 1: The star or asterisk is known as a whild card.

Why would I want to delete multiple files at once?

Great question! Well let me give you an example of something that happened to me yesterday. I being the huge movie collecter that I am have over 1000 audio described movies within my movies folder on my external hard drive. and there are quite afew coppies of the same movie. The diffirence? They have an dot OGG extention. I didn't want those movies, so what did I do? That's right, I deleted them using the nifty del space star dot ogg command and poof! they were all gone!

Thanks for reading,

Thumbs up


Ipconfig with its various switches is quite useful. But I'm sure nobody wants to hear about that.

However, while this is probably old news to pretty much everyone as well, I do rather enjoy the switches that can do useful things with Skype.

For example, cd into the directory where Skype is contained, and then, after typing skype.exe, make sure to type a space, then /secondary. This will open another instance of Skype, where you can log into another account, if you have multiple accounts. You can also do /minimized to start your current Skype session minimized, also /username:your username, and then /password:your password to log in with the account. Sure, it's a lot more typing than just launching the desktop app, but it's still neat that you can do these things. You can also do /shutdown in order to end a running Skype instance.

So an example would look like this:

cd C:\Program Files (x86)\Skype\Phone\Skype.exe /minimized

Nice topic, I expect to learn a lot of stuff I had no clue about before.

The glass is neither half empty nor half full. It's just holding half the amount it can potentially hold.

Thumbs up


I wish to introduce four new topics to this topic. They are piping, redirection, jobs, and multi-command stacking.
Piping is the process of taking the output of one program and passing it as the arguments to another program. An example would be passing a large amount of output from, say, cat to more, a command-line and easy-to-use pager:
cat /etc/passwd | more
The vertical bar (or pipe) (|) character is used to indicate that you wish to pipe the output from one program to another.
The syntax is as follows:
program | program
Piping can also be used to create what is known as a pipe chain. Pipe chains are multiple programs piped to one after another in order. The syntax is as follows:
program | program | program | program | program | program | program | <...>
The chain can be an indefinite amount of piped programs, although you are usually limited by your shells maximum input length (which is usually 8,192 characters).
Redirection is the process of either directing the data from one file (input redirection) to a program as input or directing the output of a program (output redirection) to a file. Input redirection comes in only one form, while output redirection comes in four forms. The left angle bracket (<) and the right angle bracket (>) are used to control this redirection. The syntax for input redirection is:
program < file
where "program" is a program to execute and "file" is the redirected file. The syntax for output redirection is a bit more complicated, but is below:
Redirect STDOUT to a file, recreating the file and writing the new data into the recreated file:
program > file
Redirect STDERR to a file, recreating the file and writing the new data into the recreated file:
program 2> file
Redirect STDOUT to a file, appending the contents of STDOUT to that file without erasing the previously existing content of the file:
program >> file
Redirect STDERR to a file, appending the contents of STDERR to that file without erasing the previously existing content of the file:
program 2>> file
Note: Whitespace does matter when using this type of redirection. For instance, > > is considered two separate things than >> is. Similarly, 2 >>, 2> >, and 2 > > are all separate entities, while 2>> is not (and is used as a normal STDERR redirection operator).
Jobs are a very interesting topic, and are widely used on Linux shells such as BASH. They are not available on Windows. To create a job, run the following command, replacing program and [arguments] with the actual program name and arguments:
program [arguments] &
For instance, to compile a massive program that uses a makefile in the foreground (but allowing you to do other things while the compilation goes):
make -s &
The ampersand (&) character is used to signify that you wish for this task to be a job. After pressing enter, you will see something like:
[1] 21238
The [1] is the Job ID. This is used when terminating jobs, although PIDs are much better than job specifications. The 21238 is the process ID. Always keep this in memory; this job may need to be terminated at some point, and pressing control+C will not terminate this job like a normal programs execution would.
You can manage the jobs foregroundness and backgroundness by using the bg and fg commands. The syntax is:
Return the currently active job to the foreground:
Return the currently active job to the background:
control+Z and then bg
The control+Z key combination is used to turn a currently foreground application into a job task. You see something like:
[1] +stopped make -s
The [1] is the Job ID. The + character is used to indicate that a new job has been added to the job queue. The stopped message indicates the process state, and the make -s is the command. When a process is stopped like this, it is not truly terminated; rather, it is simply suspended -- it's memory space is still registered and in-use by the suspended task. To resume the task, use either the fg (foreground) or bg (background) commands. Passing fg or bg an argument signifies that you wish that job ID to go into the foreground (come to front) or the background (send to back). This must be a valid Job ID. Job IDs are also known as 'jobspecs'.
Multi-command stacking is the process of using conditional operators to signify what the operating system should do if a particular exit code is returned when a program exits. The exit code is an integral value that signifies the process's status when it exited. Traditionally, the exit code zero (0) indicated (and still does indicate) success, while a positive or negative exit code signified (and still does signify) an error of some kind. For you C/C++ programmers out there, the exit code is usually stored in the ERRNO constant in the parent process's memory space. In BASH, the $? variable signifies the last executed command's exit code. The operators usable in multi-command stacking are as follows:
&& (and): Execute the next command on the right-hand side of the operator only if the command on the left-hand side of the operator succeeds (exits with a success exit code).
|| (or): Execute the command on the right-hand side of the operator only if the command on the left-side of the operator exits with a non-success error code (non-zero exit code). If the process on the left-hand side of the operator exits with a success exit code, the command on the right-hand side of the operator will not be executed.
These are the only operators I know of and have used successfully.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up


Here are a couple you may find interesting.

To copy the last commands output to a text file, type that command followed by ">somefilename.txt". For example, you can do the following

ipconfig >information.txt

Also, | clip will copy the output of something to the clipboard. This is only on windows that I know of, although you can do a similar thing on linux and os X, but you'll need to install additional packages to get that working.

You can follow me on twitter @cartertemm

Thumbs up


So I've been looking for mention of PBX and Asterisk when this question came up.
Should we start making a list here of SSH and SFTP commands so people who want to start using Linux like Ubuntu on a VPS can have some sort of idea on where to begin?
When I first logged in, I was told to type 'apt-get update', where it would refresh the list of repositories and look for pacakges in the http://archive.ubuntu.com/
Once the search is complete, type 'apt-get upgrade',, which will install all of the packages that need updating.
If you want to search for a specific package using the advanced package tool, which is what apt stands for, type 'apt-cache search packagename Example: 'apt-cache search samba'. If the package exists, I believe you type 'apt-get install packagename'. Some packages have extra things. For example, if you're installing Apache, My SQL, and PHP, you'd specify the type of PHP package you want with a dash. Like this. 'apt-get install php-nameofsubpackage'
Note, I used the single quotation mark so you can see where the commands start and end. For this I am using Secure CRT, which is portable, and supports multiple protocols.

Thumbs up


@Green Gables

Correct me if I am wrong, but SecureCRT isn't free but trial software.

Putty on the other hand is free.

Thumbs up


have both cmd and bash tricks for you people.
Windows trick:
the cd command is limited to the current drive. Type the drive letter and a colon to switch to another drive, note that your current directory is drive specific! Which is sweet btw...
In windows 10, you can now press control v to paste into cmd, although putty doesn't like this. Control c still sends it's char because it kinda has to!
pushd and popd are useful, especially for working with UNC paths.
Unix style paths work on windows, for example "cd /" changes to the root of your current drive, I always find myself using / instead of backslash, which makes some programs have a fit...
if you use the dir command to list a directory and you don't like all the extra output, add a /b switch and it'll list it like ls, just the filenames and foldernames in it.
if you wish to remove a directory, use rmdir, or rd. If the directory is not empty it will yell at you, "rd /s" fixes that. Also del has a /s switch, which does recursive deletion, here's a practical example.
cd %userprofile%\documents\sfx
del /s *.sfk
del /s *.sfl
del /s *.reapeaks
you will get spammed with output, you could try using > nul, maybe? But it will delete all those stupid files audio editors generate.
A particular command I use if I wish to run a game server compiled for windows on linux, that doesn't terminate when I kill the ssh session, looks like this. Note that you need no additional packages except of course wine.
nohup wine server.exe &
the nohup makes it not receive a hang up signal, which will make it less likely to die when you close your ssh, and won't make it run in your console. The '&' on the end there is something strange that makes it run completely background, meaning you can log off and it'll keep going! To make it die, use pkill wine, or I believe pkill server.exe also works.
if you're making windows batch files, @echo off is a very common line to use, to make the command shell not log your commands but only the output.
Another command used in batch files is "setlocal enableextensions" which makes all environment variable changes local so you can store temp stuff that stays temp, and enableextensions does something that I don't really know much about.
A nice command in batch files if you wish to prompt the user for some input instead of making them specify it as an argument to the batch file, so they can press enter on it, is the following.
set /p inp="enter some text here"
rem just throw it back at them
echo %inp%
That could be used for a youtube-dl batch file... Hehe!
Windows has three different copy commands for different things. Copy, xcopy and robocopy. Use their corresponding /? switches, and make sure you redirect to a file because the output of those commands is several pages long and your cmd will flat wrap.
If you wanna shut down your computer, and be cool and/or quick about it, use
"shutdown.exe /s /t 00 /f"
you could also use /r instead of /s to make it reboot.
If you want to add a filetype, such as .py, .pl etc to your execution path so you can click on them from explorer and they will just run, once you've configured to open them with their interpreter obviously, open an admin command prompt and use this one.
set pathext="%pathext%;.stuff"
You'll have to log out and back in for that to work.
I'm still struggling with the exact syntax for the ftype and assoc commands, but they're fun to use, because they're a quick and painless way to register a file extension to be opened by a program without the windows explorer dialog crap. I actually used it once to associate all the dependency files for rhythm rage levels with notepad, because you can have more than one extension associated with the same type! You have to use multiple assoc commands though.
Again for batch file people, who  don't use it and you want to run a program that does it's own thing from a batch file, use the start command so it doesn't hang up your batch file.
the title command is useful btw, changes the title of your prompt so it can cram it's path up it's binary ass.
if you are doing batch files and you want the user to press a key before something happens, pause. It freezes your batch file until they hit a key, prompting them with "press any key to continue..."
And for people who are programming and want cmd to do something, cmd itself has two switches. /c and /k. /c executes a command and closes, /k executes a command and leaves it open, useful for examining output but unfortunately also reprompting the user with the do stuff prompt... Maybe try either using > to output stuff to a temp file or if it's possible grab it's stdout/stderr directly.
Sorry for the spam, just thought of all that shit on the fly in bursts. Enjoy!

Best Regards

Thumbs up


For the reboot, is there a similar command in a terminal?

say I do sudo grub-reboot 2, can I then do a command to reboot the system from terminal without having to power it off/back on again?

Curious, as it'll help my laptopop with dual booting here....

Thumbs up


I'll give a description of what command does; I'll leave you to find out how they work on your own, because that's how everyone best learns.

  • Find Files by Name: find -name

  • Find Files by Ownership: find -user

  • Find Files by File Size: find -size

  • Find Files by File Type: find -type

  • Find Files by Time: find {-amin|-cmin|-mmin|-atime|-ctime|-mtime|-anewer|-cnewer|-newer|-newerXY} (|=or and {/} means required)

  • Show Results If the Expressions Are True (AND): find -a

  • Show Results If Either Expression Is True (OR): find -o

  • Show Results If the Expression Is Not True (NOT): find -n

  • Execute a Command on Found Files: find -exec

  • Execute a Command on Found Files More Efficiently: find + (or) find | xargs

  • Execute a Command on Found Files Containing Spaces: find -print0 | xargs -0

  • View Your Command-Line History: history

  • Run the Last Command Again: !!

  • Run a Previous Command Using Numbers: ![##]

  • Run a Previous Command Using a String: ![string]

  • Search for a Previous Command and Run It: ^-r (Ctrl-r) (or) ^-s (Ctrl-s) (or) ^-g (Ctrl-g)

  • Display All Command Aliases: alias

  • Discover How Long Your Computer Has Been Running: uptime

I'll add more as I go along. That's certainly not all I know!

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up