command-line,terminal,quotes,double-quotes,cp , When to use quotes in command line? cp versus find

When to use quotes in command line? cp versus find


Tag: command-line,terminal,quotes,double-quotes,cp

I'm wondering if there is some pattern or trick to remember when or when not to use quotes in command line arguments.

e.g. what is the difference between:

find -type f -name "*<extension-with-quotes>"


cp <extension-without-quotes> ../<new-folder>

One needs quotes and one does not, else it gives an error. Why?


You need quotes if you don't want the shell expanding the arguments, but instead want the argument passed through verbatim to whatever program you're trying to run. See, for example, the following program:

#include <stdio.h>
int main (int argc, char *argv[]) {
    printf ("Argument count = %d\n", argc);
    for (int i = 0; i < argc; i++)
        printf ("   %2d: [%s]\n", i, argv[i]);
    return 0;

which outputs its argument count and arguments. The following transcript shows how it runs with and without quotes:

$ ./testprog "*.sh"
Argument count = 2
    0: [./testprog]
    1: [*.sh]

$ ./testprog *.sh
Argument count = 7
    0: [./testprog]
    1: []
    2: []
    3: []
    4: []
    5: []
    6: []

So, for example, if you're in a directory with three log files, the shell will change your:

ls *.log


ls a.log b.log c.log

before handing that list on to the ls program (the ls program will never see the *.log at all).

However, find expects a file pattern rather than a list of files, so it will want the *.log passed through as is, one single argument rather than three individual arguments expanded by the shell.

In fact, if you had only a.log in the current directory, an unquoted *.log would only find files called a.log regardless of how many other log files existed in the directories below. That's because find never saw the *.log, only the a.log that the shell expanded it to.

A similar example is with expr. If you want to know what three times seven is, you don't want to be doing:

expr 3 * 7

since the shell will first expand * into all the files in the current directory:

3 dallas_buyers_club.avi nsa_agent_list.txt whitehouse_bomb.odt 7

and expr won't be able to make much sense of that1. The correct way of doing it is along the lines of:

expr 3 '*' 7

in effect preserving the * so the program gets it unchanged.

1 Special note to the NSA, CIA, MPAA and other dark shadowy organisations formed to strike fear into the hearts of mortal men. That file list is fictional humour. I really don't want any men in dark suits showing up at my front door :-)


Why I'm getting different java versions

I just upgraded my Java from 1.8 update 31 to update 45. Once done, I checked in the Java Console it shows Java 8 update 45. But, when I checked in the terminal it shows java version "1.8.0_31". I checked using Verify Java Version, and it show You have the...

Running Rscript in command line and loading packages

I have a foo.R file which contains library("ggplot2") cat("Its working") I am trying to run foo.r via the command line using the Rscript commandRscript --default-packages=ggplot2 foo.R and it is giving me the following error: 1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no...

Increment Serial Number using EXIF

I am using ExifTool to change the camera body serial number to be a unique serial number for each image in a group of images numbering several hundred. The camera body serial number is being used as a second place, in addition to where the serial number for the image...

If exist and errorlevels in a batch (.bat) file

I'm trying to run a delete command on the result of an SQL command but only if it returns an error code of 0. Below is my code: SqlCmd command... REM if SqlCmd command is successful run the below if exist statement if errorlevel 0 ( REM if the file...

Taking multiple header (rows matching condition) and convert into a column

Hello I have a file that has multiple Headers in it that I need to have turned into column values. The file looks like this: Day1 1,Smith,London 2,Bruce,Seattle 5,Will,Dallas Day2 1,Mike,Frisco 4,James,LA I would like the file to end up looking like this: Day1,1,Smith,London Day1,2,Bruce,Seattle Day1,5,Will,Dallas Day2,1,Mike,Frisco Day2,4,James,LA The file...

Via Windows command line, how can we compile a Netbeans C/C++ application?

Let's take this simple C/C++ application Netbeans project folder. In Netbeans IDE, we just hit build button on the toolbar to build the application. I want to do that automatically via Windows command line, how can I do that? I did google, and found some related posts though not very...

How do i make a condition that will end the program, and prevent further cout statements?

I'm currently making a text-based game just for kicks, which gives the user three options to choose from. One of the options allows the user to continue, while the others end the game with a "GAME OVER" text. I thought that making an endGame() function would do the trick, but...

Is there a way to go two directories back in cygwin (linux)?

I know that you can go back one directory in cygwin by using the dash (-). But, how do you go back more than one directory in your history? I've tried cd -- it doesn't seem to work. Thanks! This would be very very helpful. ...

Macports switch PHP CLI version

I'm trying to switch my Terminal PHP version to 5.4 because I ran into some issues with Drush while updating my Drupal core. The reason for these issues is my Terminal PHP version is different then my localhost. php -v in Terminal returns PHP 5.5.13 (cli) but my localhost...

How to release binaries on Github for different plattforms as seperate downloads?

I found this at github Their page provide a way to attach binaries to a special release. My build script compiles binaries for Windows MacOS and Linux. How to supply seperate downloads for different platforms there? AS far I can see these releases are simple git tags. Is it...

Continuous data stream from linux command line into python script

I need to get a continuous stream of data flowing from a peripheral device, to a raspberry pi, and lastly to my computer (mac). So far, the data is can make its way continuously (in real time) from the peripheral, to the command line of my raspberry pi. Note that...

The command “sudo apt-get install python-dev, python3-dev” failed and exited with 100 during

It seems like sudo apt-get install build-essential worked just fine but, but it gets error while installing python-pip. Here's the log of the failed build....

Using a command-line utility to perform the following map-updates

I'm a complete newbie to using command-line utilities and am wondering how to process information as following: mapping.txt: 80 001 002 81 011 012 013 014 82 021 022 ... input.txt: 81 103823044 80 103823054 81 103823064 ... Desired output.txt: 103823044|011| 103823044|012| 103823044|013| 103823044|014| 103823054|001| 103823054|002| 103823064|011| 103823064|012| 103823064|013| 103823064|014|...

vbscript to run command

I'm trying to create a script that runs the command: RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 Right now this errors out Dim objShell Set objShell = Wscript.CreateObject("WScript.Shell") sEXE = """C:\Windows\system32\RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 """ with CreateObject("WScript.Shell") .Run sEXE & " " , 1, true end with Wscript.Quit Any suggestions? Could I make the script...

AWK count number of times a term appear with respect to other columns

Given a CSV file: id, fruit, binary 1, apple, 1 2, orange, 0 3, pear, 1 4, apple, 0 5, peach, 0 6, apple, 1 How can i calculate for each unique values in fruit, the number of times the binary value =1 / number of occurences of that fruit...

How can I put a python program with multiple files in /usr/local/bin?

I know that if my program were just one python script file, I could just start it with the shebang and put it in /usr/local/bin so that I could invoke it at any time from the command prompt. However, what if my program were multiple files, but I only want...

How to make a scrolling menu in python-curses

There is a way to make a scrolling menu in python-curses? I have a list of records that I got from a query in sqlite3 and I have to show them in a box but they are more than the max number of rows: can I make a little menu...

How can I run the Jenkins jobs through terminal?

I heard an version that webhooks could be used for this. Please help)

Why is Unix/Terminal faster than R?

I'm new to Unix, however, I have recently realized that very simple Unix commands can do very simple things to large data set very very quickly. My question is why are these Unix commands so fast relative to R? Let's begin by assuming that the data is big, but not...

Iterate over specific files in a directory using Bash find

Shellcheck doesn't like my for over find loop in Bash. for f in $(find $src -maxdepth 1 -name '*.md'); do wc -w < "$f" >> $path/tmp.txt; done It suggests instead: 1 while IFS= read -r -d '' file 2 do 3 let count++ 4 echo "Playing file no. $count" 5...

Eclipse CDT - No Console Output on OSX

New to the C++ world and wanted to fiddle around using Eclipse's IDE for C/C++ called CDT. I am on OSX 10.10.2 using eclipse-cpp-luna-SR2-macosx-cocoa-x86_64. Sadly this simple example is not printing anything in the eclipse terminal. #include <iostream> using namespace std; int main() { cout << "Hello World" << endl;...

How should I add a stationary progress bar to a C++ program that produces terminal output (in Linux)?

I have an existing program that contains a loop over files. It does various things, providing lots of terminal output. I want to have an overall progress bar that remains stationary on the same line at the bottom of the terminal while all of the output from the file operations...

Gradle Assemble Debug is Successful But Does Not Work [duplicate]

This question already has an answer here: Why won't gradle run from within a jenkins job 2 answers After following instructions here: apparently you're supposed to chmod +x gradlew then ./gradlew assembleDebug which leads to the following output: /gradlew assembleDebug :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72200Library...

Why is csc.exe crashing when I last left the output encoding as UTF8?

I am having am having or have run into a very strange thing. I wonder if others have and why it's happening. Having run a one line program with this line System.Console.WriteLine(System.Console.OutputEncoding.EncodingName); I see the Encoding is Western European (DOS) Fine Here is a list of some codepages 1200 Unicode...

Grails 3.0.2 missing generate-views

I'm new to grails and I'm trying to get my first HelloWorld from it. I can generate it with the controller, but I can't create a view because it isn't listed in the Grails (3.0.2) list of command. Because any IDE support grails 3.*, I can't create it by that...

command line Start.Info.Argument error

I would like run this command with C# : "%WIX%/bin/heat.exe" dir "C:\Documents and Settings\APP" -gg -sfrag -cg Appli -out wixappli.wxs I have this code (Program.cs) : System.Diagnostics.Process process1 = new System.Diagnostics.Process(); process1.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe"); process1.StartInfo.Arguments = String.Format("/k \"%WIX%/bin/heat.exe\" dir \"{0}\" -gg -sfrag -cg Appli -out wixappli.wxs ", @"C:\Documents and...

Error in reading Ubuntu 14.04 mouse event file (/dev/input/event3) with java programmig

I want to handle mouse event in Linux terminal via java programming. I wrote two program via c++ and java that they do same process. when i use c++ programming to open and read file ("/dev/input/event3"-mouse event file), there is no problem while running executable file. (Ubuntu 14.04 terminal and...

How do I silence the HEAD of a curl request while using the silent flag?

When I run the curl command and direct the data to a file, I get back the content of the site as expected. $ curl "" > file.txt $ head file.txt Top of site ... However, this command shows a progress bar, which I do not want: % Total %...

Importing a mysql database with command line

Have tried all other answers on stackoverflow, and they haven't worked. I have a database in phpMyAdmin, and I need to update it. The file is too big to upload via phpMyAdmin, and I've tried increasing limits in php.ini to no avail. I think that didn't work because I updated...

cat /dev/null to multiple files to clear existing files like logs

A good way to clear logs(syslog has a handle on file) which have frozen my linux server(out of space) I tried cat /dev/null > fileABC; cat /dev/null/ > fileXYZ How can I clear multiple files by cat /dev/null to multiple files in an efficient or single command. ...

Set Java path in command line for only one directory

I am running a program that utilizes Scala 2.10 for work and is not compatible with Java 8, only Java 7. In a Windows 7 command line, how can I set the java path to use Java 7 ONLY for that directory?

Reading from linux command line with Python

Is there a way to read data that is coming into the command-line, straight into another Python script for execution?

Recursively copy /dev/null to file pattern

I am using a ZFS system that has reached its disk quota. As a result, the rm command no longer is available to me. However, I am able to copy /dev/null to particular files to effectively remove them. The issue is that the directory I am trying to clear out...

Get the output from executed commands through android app

I'm performing the following code to execute linux commands in my android application that I'm creating: public void RunAsRoot(String[] cmds){ Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"\n"); } os.writeBytes("exit\n"); os.flush(); } I want to know if there is a way to...

Mac Terminal Auto Complete

I have been looking for a while for a patch for this. Usually on a Unix/Linux terminal when you press tab it will auto-complete until there are several options and then it will list the options below for you to select. For example: cd he helpFolder/ helpMe/ heIsThere/ cd help...

Using the Rails console to delete odd-numbered records

I'd like to delete records with odd numbered ID's from Hero table using the Rails console. Would someone be so kind as to suggest a way for me to do this? Kind Regards, Ryan...

How to set the classpath in Windows Command Line correctly

I have made many attempts to enter a command to run a JAR file correctly from the terminal, and I am 100% sure that all the JAR files and such are in the given paths, but I keep getting a ClassDefNotFoundException. Does it have anything to do with the way...

Is there a way to save a mac terminal output directly to a file?

I am carrying out a research project for which I need to do quite a lot of calculations. I basically calculated a lot of features for structures present in a .sdf file. I automated the entire process and carried it out in the terminal of my mac. The thing is,...

How to compose Python exe programs using pipes?

Python newbie here. I created a Python program,, which outputs a message (Hello, World). I created a Python program,, which reads input and outputs it. Using py2exe I created an executable of each of them: msg.exe and id.exe I created a DOS batch file, set PATH to the...

How to uninstall a program using C#? [duplicate]

This question already has an answer here: How to uninstall MSI using its Product Code in c# 4 answers I'm trying to uninstall a program using C# via Visual Studio and possibly CMD. I made several attempts but could not getting anything to go. Attempt #1: RegistryKey localMachine =...

xargs echo colored output

I have the following command: somethingRegex | xargs -I {} sh -c 'echo -e "found \e[34m{}\e[39m";dummy {}' The color part of the echo does not work, example output: -e found \e[34mresult\e[39m dummy output repeat A plain echo does work with {} being nice blue echo -e "found \e[34m{}\e[39m" How do...

How to exit if statement in bash without exiting program?

Let's say I have a script which is executable. It looks like this: if true then echo "started if statement" exit fi echo "hello" When I run this script, hello is not printed, because exit exits the entire shell. Is there a way to make exit (or some other statement)...

loop through different arguments in Rscript within Korn shell

I have an R script which I'm running in the terminal by firstly generating a .ksh file called myscript.ksh with the following information: #!/bin/ksh Rscript myscript.R 'Input1' and then run the function with ./mycode.ksh which sends the script to a node on the cluster in our department (the processes that...

How to list the contents of a subdirectory after finding the directory

This is a rather specific question, but essentially what I was wondering is if there is anyway to list the contents of a directory below the one that I search for. My case is that I have a parent directory with a ton of subdirectories that I want to search...

How to get CPU utilization in % in terminal (mac)

Ive seen the same question asked on linux and windows but not mac (terminal). Can anyone tell me how to get the current processor utilization in %, so an example output would be 40%. Thanks

Terminal line resetting after about 50 characters

My command line prompt will reset after around 50 characters, including the prompt. For example, if the prompt looks like: [email protected]$ and I start to type [email protected]$ blah blah blah blah blah blah I'll eventually get to the point where the command line starts to reset like so: blah blmputer$...

running testng.xml via command line- error Cannot find class in classpath: com.companyname.SSProject.LaunchSSProject

I am using following command on command line: C:\Users\MyScriptDirectory>java -classpath ".;Lib\selenium-2.45.0\libs\testng-6.8.5.jar" org.testng.TestNG testng.xml Getting error message: [TestNG] [Error] Cannot find class in classpath: com.companyname.SSProject.LaunchSSProject I have made following entry in testng.xml: <class name="com.companyname.SSProject.LaunchSSProject" /> "com.companyname.SSProject" is my package having file inside it When I am running the same testng.xml...

Redirect stdout using exec in subscript

I have one main script which is started as an service. I can't modify this main script, because it is often updated. This main script starts a program, which echo any log to stdout. So i can't see any log of this program. But this main script calls at the...